<!DOCTYPE html> <html><!--
 Page saved with SingleFile 
 url: https://mp.weixin.qq.com/s?__biz=MzI4Njg5MDA5NA==&mid=2247484109&idx=1&sn=ed48fa9216c260fb9b622d9f383d8c25&chksm=ebd743ccdca0cadad9e8e4a5cd9a7ce96b595ddaf6fb2e817a9a0d49d4d54c50bb93a97e56eb&scene=21###wechat_redirect 
 saved date: Thu Nov 28 2019 20:27:03 GMT+0800 (中国标准时间)
--><meta charset=utf-8>
<meta http-equiv=X-UA-Compatible content="IE=edge">
<meta name=viewport content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0,viewport-fit=cover">
<meta name=apple-mobile-web-app-capable content=yes>
<meta name=apple-mobile-web-app-status-bar-style content=black>
<meta name=format-detection content="telephone=no">
<meta name=description content=泛型就这么简单～回顾泛型的基础>
<meta name=author content=Java3y>
<meta property=og:title content=泛型就这么简单>
<meta property=og:url content="http://mp.weixin.qq.com/s?__biz=MzI4Njg5MDA5NA==&amp;mid=2247484109&amp;idx=1&amp;sn=ed48fa9216c260fb9b622d9f383d8c25&amp;chksm=ebd743ccdca0cadad9e8e4a5cd9a7ce96b595ddaf6fb2e817a9a0d49d4d54c50bb93a97e56eb#rd">
<meta property=og:image content="http://mmbiz.qpic.cn/mmbiz_jpg/2BGWl1qPxib2qYPoJkJPxUx0guZHy8QibLIIdFbbsIfYwpCX666lLDwSldo5MibbXqzdaAlMh3UicuOhlkuYXcMUug/0?wx_fmt=jpeg">
<meta property=og:description content=泛型就这么简单～回顾泛型的基础>
<meta property=og:site_name content=微信公众平台>
<meta property=og:type content=article>
<meta property=og:article:author content=Java3y>
<meta property=twitter:card content=summary>
<meta property=twitter:image content="http://mmbiz.qpic.cn/mmbiz_jpg/2BGWl1qPxib2qYPoJkJPxUx0guZHy8QibLIIdFbbsIfYwpCX666lLDwSldo5MibbXqzdaAlMh3UicuOhlkuYXcMUug/0?wx_fmt=jpeg">
<meta property=twitter:title content=泛型就这么简单>
<meta property=twitter:creator content=Java3y>
<meta property=twitter:site content=微信公众平台>
<meta property=twitter:description content=泛型就这么简单～回顾泛型的基础>
<title>泛型就这么简单</title>
<style>.rich_media_inner{word-wrap:break-word;-webkit-hyphens:auto;-ms-hyphens:auto;hyphens:auto}.rich_media_area_primary{padding:calc(20px + env(safe-area-inset-top)) calc(16px + env(safe-area-inset-right)) 12px calc(16px + env(safe-area-inset-left))}.rich_media_area_extra{padding:0 calc(16px + env(safe-area-inset-right)) calc(16px + env(safe-area-inset-bottom)) calc(16px + env(safe-area-inset-left))}html{-ms-text-size-adjust:100%;-webkit-text-size-adjust:100%;line-height:1.6}body{-webkit-touch-callout:none;color:#333;background-color:#f2f2f2;letter-spacing:.034em}h2{font-weight:400}*{margin:0;padding:0}a{-webkit-tap-highlight-color:rgba(0,0,0,0)}.rich_media_title{font-size:22px;line-height:1.4;margin-bottom:14px}@supports (-webkit-overflow-scrolling:touch){.rich_media_title{font-weight:700}}.rich_media_meta_list{margin-bottom:22px;line-height:20px;font-size:0;word-wrap:break-word;word-break:break-all}.rich_media_meta_list em{font-style:normal}.rich_media_meta{display:inline-block;vertical-align:middle;margin:0 10px 10px 0;font-size:15px;-webkit-tap-highlight-color:rgba(0,0,0,0)}.rich_media_meta.meta_tag_text{margin-right:0}.rich_media_meta_text{color:rgba(0,0,0,0.3)}.rich_media_meta_nickname{position:relative}.rich_media_content{overflow:hidden;color:#333;font-size:17px;word-wrap:break-word;-webkit-hyphens:auto;-ms-hyphens:auto;hyphens:auto;text-align:justify;z-index:0}.rich_media_content *{max-width:100%!important;box-sizing:border-box!important;-webkit-box-sizing:border-box!important;word-wrap:break-word!important}.rich_media_content p{clear:both;min-height:1em}.rich_media_content .list-paddingleft-2{padding-left:2.2em}.rich_media_content img{height:auto!important}@media screen and (min-width:1024px){.rich_media_area_primary_inner,.rich_media_area_extra_inner{max-width:677px;margin-left:auto;margin-right:auto}.rich_media_area_primary{padding-top:32px}}blockquote{padding-left:10px;border-left:3px solid #dbdbdb;padding-top:4px;margin:1em 0}.weui-flex{display:-webkit-box;display:-webkit-flex}.weui-flex__item{-webkit-box-flex:1;-webkit-flex:1}.appmsg_skin_default .rich_media_area_primary{background-color:#fff}.appmsg_style_default .rich_media_tool{padding-top:15px}.read-more__area{margin:30px 0}html,body{height:100%}</style>
<!--[if lt IE 9]>
<link rel="stylesheet" type="text/css" href="//res.wx.qq.com/mmbizwap/zh_CN/htmledition/style/page/appmsg_new/pc492bcc.css">
<![endif]-->
<style id=page/appmsg_new/not_in_mm.css>.weui-flex{display:-webkit-box;display:-webkit-flex;display:flex}.weui-flex__item{-webkit-box-flex:1;-webkit-flex:1;flex:1}html{-ms-text-size-adjust:100%;-webkit-text-size-adjust:100%}body{line-height:1.6;font-family:-apple-system-font,BlinkMacSystemFont,"Helvetica Neue","PingFang SC","Hiragino Sans GB","Microsoft YaHei UI","Microsoft YaHei",Arial,sans-serif;font-size:16px}body,h1,h2,h3,p,ul,ol{margin:0}a{color:#576b95;text-decoration:none}body,html{-webkit-appearance:none;-webkit-tap-highlight-color:rgba(0,0,0,0)}@media all and (orientation:portrait){@-webkit-keyframes opr_fade_out{0%{filter:alpha(opacity = 100);-moz-opacity:1;-khtml-opacity:1;opacity:1}100%{filter:alpha(opacity = 0);-moz-opacity:0;-khtml-opacity:0;opacity:0}}@-webkit-keyframes opr_fade_in{0%{filter:alpha(opacity = 0);-moz-opacity:0;-khtml-opacity:0;opacity:0}100%{bottom:0;filter:alpha(opacity = 100);-moz-opacity:1;-khtml-opacity:1;opacity:1}}}@-webkit-keyframes opr_fade_out{0%{filter:alpha(opacity = 100);-moz-opacity:1;-khtml-opacity:1;opacity:1}100%{filter:alpha(opacity = 0);-moz-opacity:0;-khtml-opacity:0;opacity:0}}@-webkit-keyframes opr_fade_in{0%{filter:alpha(opacity = 0);-moz-opacity:0;-khtml-opacity:0;opacity:0}100%{filter:alpha(opacity = 100);-moz-opacity:1;-khtml-opacity:1;opacity:1}}@-webkit-keyframes opacity-60-25-0-12{0%{opacity:.25}0.01%{opacity:.25}0.02%{opacity:1}60.01%{opacity:.25}100%{opacity:.25}}@-webkit-keyframes opacity-60-25-1-12{0%{opacity:.25}8.34333%{opacity:.25}8.35333%{opacity:1}68.3433%{opacity:.25}100%{opacity:.25}}@-webkit-keyframes opacity-60-25-2-12{0%{opacity:.25}16.6767%{opacity:.25}16.6867%{opacity:1}76.6767%{opacity:.25}100%{opacity:.25}}@-webkit-keyframes opacity-60-25-3-12{0%{opacity:.25}25.01%{opacity:.25}25.02%{opacity:1}85.01%{opacity:.25}100%{opacity:.25}}@-webkit-keyframes opacity-60-25-4-12{0%{opacity:.25}33.3433%{opacity:.25}33.3533%{opacity:1}93.3433%{opacity:.25}100%{opacity:.25}}@-webkit-keyframes opacity-60-25-5-12{0%{opacity:.270958333333333}41.6767%{opacity:.25}41.6867%{opacity:1}1.67667%{opacity:.25}100%{opacity:.270958333333333}}@-webkit-keyframes opacity-60-25-6-12{0%{opacity:.375125}50.01%{opacity:.25}50.02%{opacity:1}10.01%{opacity:.25}100%{opacity:.375125}}@-webkit-keyframes opacity-60-25-7-12{0%{opacity:.479291666666667}58.3433%{opacity:.25}58.3533%{opacity:1}18.3433%{opacity:.25}100%{opacity:.479291666666667}}@-webkit-keyframes opacity-60-25-8-12{0%{opacity:.583458333333333}66.6767%{opacity:.25}66.6867%{opacity:1}26.6767%{opacity:.25}100%{opacity:.583458333333333}}@-webkit-keyframes opacity-60-25-9-12{0%{opacity:.687625}75.01%{opacity:.25}75.02%{opacity:1}35.01%{opacity:.25}100%{opacity:.687625}}@-webkit-keyframes opacity-60-25-10-12{0%{opacity:.791791666666667}83.3433%{opacity:.25}83.3533%{opacity:1}43.3433%{opacity:.25}100%{opacity:.791791666666667}}@-webkit-keyframes opacity-60-25-11-12{0%{opacity:.895958333333333}91.6767%{opacity:.25}91.6867%{opacity:1}51.6767%{opacity:.25}100%{opacity:.895958333333333}}@-webkit-keyframes loading{0%{-webkit-transform:rotate3d(0,0,1,0deg)}100%{-webkit-transform:rotate3d(0,0,1,360deg)}}@keyframes loading{0%{-webkit-transform:rotate3d(0,0,1,0deg)}100%{-webkit-transform:rotate3d(0,0,1,360deg)}}@supports (-webkit-overflow-scrolling:touch){.reward_button{font-weight:700}}.rich_media_extra{position:relative}.top_banner{background-color:#fff}.ct_mpda_wrp{margin:38px 0 20px}.like_btn{-webkit-appearance:none;-webkit-tap-highlight-color:rgba(0,0,0,0);outline:0;background-color:transparent;border:0;display:inline-block;vertical-align:middle;padding:0;font-size:15px;font-family:inherit;line-height:2.1333333333333333;color:#576b95}.like_btn:before{font-size:16px;content:"";display:inline-block;width:1em;height:1.125em;vertical-align:middle;margin-top:-0.25em;margin-right:.05em;background:transparent url("data:image/svg+xml;charset=utf8, %3Csvg width='18' height='20' viewBox='0 0 18 20' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M5.485 3.785l2.506-2.477a1.674 1.674 0 0 1 2.352 0l2.505 2.477 3.423.908a1.653 1.653 0 0 1 1.18 2.026l-.917 3.382.918 3.384a1.652 1.652 0 0 1-1.18 2.024l-3.399.902-2.53 2.493a1.674 1.674 0 0 1-2.352 0l-2.506-2.477-3.423-.908a1.653 1.653 0 0 1-1.18-2.026l.917-3.383-.918-3.392a1.652 1.652 0 0 1 1.18-2.025l3.424-.908zm.836 1.447l.006 2.298c0 .59-.317 1.138-.828 1.438l-2.015 1.143 2.005 1.136c.517.29.838.841.838 1.435l-.006 2.298 2.01-1.156a1.667 1.667 0 0 1 1.675-.003l2.007 1.154-.007-2.302c0-.583.319-1.13.829-1.43l2.014-1.142-2.005-1.136a1.647 1.647 0 0 1-.838-1.435l.007-2.298-2.01 1.156a1.65 1.65 0 0 1-1.67.001L6.321 5.232zm-1.094 2.3L5.22 4.994l-2.878.763a.552.552 0 0 0-.398.674l.77 2.851 2.23-1.264a.573.573 0 0 0 .283-.486zm-.278 4.673L2.714 10.94l-.77 2.84a.553.553 0 0 0 .399.676l2.877.763.007-2.537a.548.548 0 0 0-.278-.476zm3.935 2.57l-2.216 1.274 2.096 2.073c.222.22.583.22.806 0l2.103-2.073-2.214-1.274a.57.57 0 0 0-.575 0zm4.222-2.104l.007 2.538 2.879-.763a.552.552 0 0 0 .398-.674l-.771-2.843-2.23 1.265a.57.57 0 0 0-.283.477zm.279-4.664l2.234 1.266.77-2.84a.553.553 0 0 0-.399-.676l-2.877-.763-.007 2.537c0 .196.107.38.279.476zm-4.501-2.57c.176.104.39.104.566 0l2.215-1.274L9.57 2.09a.574.574 0 0 0-.805 0L6.668 4.163l2.216 1.274z' fill='%23576B95' fill-rule='evenodd'/%3E%3C/svg%3E") 0 0 no-repeat;background-size:1em}.like_num{font-size:15px;margin-left:.2em}.like_comment_wrp{font-size:17px;margin-top:9px;margin-bottom:8px;position:relative;z-index:1}.like_comment_wrp:before,.like_comment_wrp:after{content:"";display:inline-block;width:0;height:0;border-width:7px;border-style:dashed;border-color:transparent;border-top-width:0;border-bottom-color:rgba(0,0,0,0.03);border-bottom-style:solid;position:absolute;top:-7px;right:28px}.like_comment_wrp:after{border-bottom-color:#f7f7f7;top:-6px}.like_comment_inner{background-color:rgba(0,0,0,0.03);border-radius:4px;overflow:hidden;padding:24px 16px;display:-webkit-box;display:-webkit-flex;display:flex;-webkit-box-align:center;-webkit-align-items:center;align-items:center;text-align:center}.like_comment_primary_wrp{font-size:16px;margin-top:9px;margin-bottom:4px;background-color:#fff;z-index:21}.like_comment_primary_wrp:before,.like_comment_primary_wrp:after{content:"";display:inline-block;width:0;height:0;border-width:7px;border-style:dashed;border-color:transparent;border-top-width:0;border-bottom-color:#fff;border-bottom-style:solid;position:absolute;top:-7px;right:28px}.like_comment_primary_wrp:after{border-bottom-color:#fff;top:-6px}.like_comment_primary_wrp.editing{position:fixed;top:10px;bottom:0;left:0;right:0;margin:0}.like_comment_primary_wrp.editing:before,.like_comment_primary_wrp.editing:after{display:none}.like_comment_primary_mask{position:fixed;z-index:20;top:0;left:0;bottom:0;right:0;background-color:rgba(0,0,0,0.2)}@-webkit-keyframes weuiLoading{0%{-webkit-transform:rotate3d(0,0,1,0deg)}100%{-webkit-transform:rotate3d(0,0,1,360deg)}}@keyframes weuiLoading{0%{-webkit-transform:rotate3d(0,0,1,0deg)}100%{-webkit-transform:rotate3d(0,0,1,360deg)}}@-webkit-keyframes slidein{from{-webkit-transform:translateX(-50%);transform:translateX(-50%)}to{-webkit-transform:translateX(0);transform:translateX(0)}}@keyframes slidein{from{-webkit-transform:translateX(-50%);transform:translateX(-50%)}to{-webkit-transform:translateX(0);transform:translateX(0)}}.mpda_bottom_container{position:relative}.rich_media_tool{overflow:hidden;line-height:32px}.rich_media_tool .meta_primary{float:left}.rich_media_tool .meta_extra{float:right}.rich_media_tool .meta_praise{text-align:right}.media_tool_meta i{vertical-align:0;position:relative;top:1px}.meta_praise{-webkit-tap-highlight-color:rgba(0,0,0,0);outline:0}.meta_praise .praise_num{display:inline-block;vertical-align:top}.meta_praise:hover{cursor:pointer}.icon_praise_gray{background:transparent url() no-repeat 0 0;display:inline-block}.rich_media_tool{font-size:15px}.rich_media_tool .meta_primary{margin-right:16px}.rich_media_tool .meta_extra{margin-left:16px;color:#576b95}.rich_media_tool .meta_praise{min-width:2.5em}.rich_media_tool .meta_praise i{margin-right:5px}.icon_praise_gray{background-image:url("data:image/svg+xml;charset=utf8, %3Csvg width='16' height='16' viewBox='0 0 16 16' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M2.5 6.988h-.003c-.095-.01-.167-.022-.125-.022H1.75c-.343 0-.75.39-.75.7v6.73c0 .31.27.57.611.57H2.5V7.01a.51.51 0 0 1 0-.022zm1 .003a.55.55 0 0 1 0 .02v7.955h7.414c.748 0 1.395-.361 1.773-1.324a37.17 37.17 0 0 0 1.115-2.57c.219-.564.413-1.11.575-1.627.247-.785.413-1.48.484-2.058.073-.595-.565-1.021-1.236-1.021h-4.97l.102-.586.18-1.027.13-.55a35.058 35.058 0 0 0 .245-1.128c.212-1.098-.483-2.019-1.238-2.067-.74-.048-1.1.111-1.104.562-.008 1.276-.45 2.805-1.252 4.129-.357.589-.899.965-1.56 1.16-.217.065-.438.107-.658.132zm6.345-1.625h3.78c1.19 0 2.393.804 2.229 2.143-.08.646-.26 1.397-.523 2.235-.17.54-.37 1.107-.597 1.69a38.158 38.158 0 0 1-1.133 2.61c-.525 1.346-1.557 1.922-2.687 1.922H1.61c-.886 0-1.611-.698-1.611-1.57v-6.73c0-.871.864-1.7 1.75-1.7l.719.009A3.285 3.285 0 0 0 3.876 5.9c.435-.13.769-.361.986-.72.71-1.171 1.102-2.525 1.108-3.618C5.978.338 6.901-.07 8.14.01c1.36.088 2.48 1.57 2.155 3.255a36.012 36.012 0 0 1-.253 1.167l-.124.52-.072.414z' fill='%23576B95' fill-rule='nonzero'/%3E%3C/svg%3E");font-size:16px;width:1em;height:1em;background-size:1em}.praise_num{color:#576b95}a,button{cursor:pointer}.rich_media_extra{overflow:hidden}.rich_media_extra_discuss{padding-top:0}.praise_num:empty{margin-left:-3px}.comment_primary_emotion_panel_wrp{position:absolute;z-index:1;padding-top:8px;padding-bottom:16px}.comment_primary_emotion_panel{background:#fff;box-shadow:0 2px 8px 0 rgba(0,0,0,0.16);border-radius:4px;width:376px;height:216px;overflow-y:auto;-webkit-overflow-scrolling:touch}.tips_global_primary{color:rgba(0,0,0,0.3)}.weui-dialog{position:fixed;z-index:5000;top:50%;left:16px;right:16px;-webkit-transform:translate(0,-50%);transform:translate(0,-50%);background-color:#fff;text-align:center;border-radius:12px;overflow:hidden;display:-webkit-box;display:-webkit-flex;display:flex;-webkit-flex-direction:column;-webkit-box-orient:vertical;-webkit-box-direction:normal;flex-direction:column;max-height:90%}@media screen and (min-width:352px){.weui-dialog{width:320px;margin:0 auto}}.weui-toast{position:fixed;z-index:5000;width:120px;height:120px;top:40%;left:50%;-webkit-transform:translate(-50%,-50%);transform:translate(-50%,-50%);background:rgba(17,17,17,0.7);text-align:center;border-radius:5px;color:#fff;display:-webkit-box;display:-webkit-flex;display:flex;-webkit-box-orient:vertical;-webkit-box-direction:normal;-webkit-flex-direction:column;flex-direction:column;-webkit-box-align:center;-webkit-align-items:center;align-items:center;-webkit-box-pack:center;-webkit-justify-content:center;justify-content:center}.weui-mask{position:fixed;z-index:1000;top:0;right:0;left:0;bottom:0;background:rgba(0,0,0,0.6)}.weui-mask_transparent{position:fixed;z-index:1000;top:0;right:0;left:0;bottom:0}@-webkit-keyframes weuiLoading{0%{-webkit-transform:rotate3d(0,0,1,0deg);transform:rotate3d(0,0,1,0deg)}100%{-webkit-transform:rotate3d(0,0,1,360deg);transform:rotate3d(0,0,1,360deg)}}@keyframes weuiLoading{0%{-webkit-transform:rotate3d(0,0,1,0deg);transform:rotate3d(0,0,1,0deg)}100%{-webkit-transform:rotate3d(0,0,1,360deg);transform:rotate3d(0,0,1,360deg)}}@media screen and (max-width:1023px){.profile_container{display:none!important}}.weui-desktop-popover{white-space:normal;word-wrap:break-word;-webkit-hyphens:auto;-ms-hyphens:auto;hyphens:auto;z-index:500;color:#353535;line-height:1.6;background:#fff;border-radius:2px}.weui-desktop-popover:before{content:" ";width:8px;height:8px;background-color:#fff;box-shadow:0 2px 10px 0 #d4d4d4;transform:matrix(0.71,0.71,-0.71,0.71,0,0);-ms-transform:matrix(0.71,0.71,-0.71,0.71,0,0);-webkit-transform:matrix(0.71,0.71,-0.71,0.71,0,0);position:absolute}.weui-desktop-popover:after{content:" ";background-color:#fff;position:absolute}.weui-desktop-popover_img-text{text-align:center}.weui-desktop-popover_pos-up-center{margin-top:16px}.weui-desktop-popover_pos-up-left:before,.weui-desktop-popover_pos-up-center:before,.weui-desktop-popover_pos-up-right:before{top:-4px}.weui-desktop-popover_pos-up-left:after,.weui-desktop-popover_pos-up-center:after,.weui-desktop-popover_pos-up-right:after{height:10px;top:0;left:0;right:0}.weui-desktop-popover_pos-up-center:before,.weui-desktop-popover_pos-down-center:before{margin-left:-4px}.weui-desktop-popover{position:absolute;padding:14px;box-shadow:none;border:1px solid #d9dadc;width:182px;box-sizing:border-box}.weui-desktop-popover:before{box-shadow:none;border:1px solid #d9dadc}.not_in_mm .rich_media_meta_list{position:relative;z-index:1}.not_in_mm .rich_media_content{position:relative}.not_in_mm .profile_container{width:535px;position:absolute;top:100%;left:0;margin-top:10px;font-size:14px}.not_in_mm .profile_inner{position:relative;padding:30px 22px 36px 144px;background-color:#fff;border:1px solid #d9dadc}.not_in_mm .profile_arrow_wrp{position:absolute;left:22px;top:-8px}.not_in_mm .rich_media_inner{position:relative}.not_in_mm .qr_code_pc_outer{display:none!important;position:fixed;left:0;right:0;top:20px;color:#717375;text-align:center}.not_in_mm .qr_code_pc_inner{position:relative;width:740px;margin-left:auto;margin-right:auto}.not_in_mm .qr_code_pc{position:absolute;right:-140px;top:0;width:140px;padding:16px;border:1px solid #d9dadc;background-color:#fff;word-wrap:break-word;word-break:break-all}.not_in_mm .qr_code_pc p{font-size:14px;line-height:20px}.not_in_mm .qr_code_pc_img{width:102px;height:102px}@media screen and (min-width:1024px){.not_in_mm .qr_code_pc_outer{display:block!important;top:32px}}.not_in_mm .qr_code_pc{box-sizing:border-box}</style><link rel="shortcut icon" type=image/x-icon href=""><style>.sf-hidden{display:none!important}</style></head>
 <body id=activity-detail class="zh_CN mm_appmsg appmsg_skin_default appmsg_style_default not_in_mm">
 
 
 
<div id=js_article class=rich_media>
 
 <div id=js_top_ad_area class=top_banner></div>
 
 <div class=rich_media_inner>
 
 
 <div id=page-content class=rich_media_area_primary>
 <div class=rich_media_area_primary_inner>
 
 
 
 <div id=img-content>
 
 <h2 class=rich_media_title id=activity-name>
 
 
 
 泛型就这么简单
 </h2>
 <div id=meta_content class=rich_media_meta_list>
 <span id=copyright_logo class="rich_media_meta rich_media_meta_text meta_tag_text">原创：</span>
 <span class="rich_media_meta rich_media_meta_text">
 Java3y
 </span>
 
 <span class="rich_media_meta rich_media_meta_nickname" id=profileBt>
 <a href="https://mp.weixin.qq.com/s?__biz=MzI4Njg5MDA5NA==&amp;mid=2247484109&amp;idx=1&amp;sn=ed48fa9216c260fb9b622d9f383d8c25&amp;chksm=ebd743ccdca0cadad9e8e4a5cd9a7ce96b595ddaf6fb2e817a9a0d49d4d54c50bb93a97e56eb&amp;scene=21" id=js_name>
 Java3y </a>
 <div id=js_profile_qrcode class=profile_container style=display:none>
 <div class=profile_inner>
 
 
 
 
 
 </div>
 <span class=profile_arrow_wrp id=js_profile_arrow_wrp>
 
 
 </span>
 </div>
 </span>
 <em id=publish_time class="rich_media_meta rich_media_meta_text">2018-04-01</em>
 </div>
 
 
 
 
 
 
 
 
 
 
 
 
 
 <div class=rich_media_content id=js_content>
 
 
 
 
 <section style="font-size:15px;color:#3e3e3e;line-height:1.8;word-spacing:2px;letter-spacing:2px;background-image:linear-gradient(90deg,rgba(50,0,0,0.05) 3%,rgba(0,0,0,0) 3%),linear-gradient(360deg,rgba(50,0,0,0.05) 3%,rgba(0,0,0,0) 3%);background-size:20px 20px;background-position:center center"><h1 style="color:inherit;line-height:inherit;margin-top:1.6em;margin-bottom:1.6em;font-weight:bold;font-size:1.6em;border-top:2px solid #039be5"><span style="font-size:inherit;line-height:inherit;display:inline-block;font-weight:normal;background:#039be5;color:#ffffff;padding-top:3px;padding-right:10px;padding-left:10px;border-bottom-right-radius:3px;border-bottom-left-radius:4px;border-bottom:2px solid #006064;border-right:3px solid #006064;margin-left:5px;margin-right:2px">前言</span></h1><p style=font-size:inherit;color:inherit;line-height:inherit;margin-top:1.7em;margin-bottom:1.7em>从今天开始进入Java基础的复习，可能一个星期会有一篇的&lt;十道简单算法&gt;，我写博文的未必都是正确的～如果有写错的地方请大家多多包涵并指正～<p style=font-size:inherit;color:inherit;line-height:inherit;margin-top:1.7em;margin-bottom:1.7em>今天要复习的是泛型，泛型在Java中也是个很重要的知识点，本文主要讲解基础的概念，并不是高深的知识，如果基础好的同学可以当复习看看～<h1 style="color:inherit;line-height:inherit;margin-top:1.6em;margin-bottom:1.6em;font-weight:bold;font-size:1.6em;border-top:2px solid #039be5"><span style="font-size:inherit;line-height:inherit;display:inline-block;font-weight:normal;background:#039be5;color:#ffffff;padding-top:3px;padding-right:10px;padding-left:10px;border-bottom-right-radius:3px;border-bottom-left-radius:4px;border-bottom:2px solid #006064;border-right:3px solid #006064;margin-left:5px;margin-right:2px">一、什么是泛型？</span></h1><p style=font-size:inherit;color:inherit;line-height:inherit;margin-top:1.7em;margin-bottom:1.7em><strong style=font-size:inherit;line-height:inherit;color:#e96900>Java泛型设计原则：只要在编译时期没有出现警告，那么运行时期就不会出现ClassCastException异常</strong>.<p style=font-size:inherit;color:inherit;line-height:inherit;margin-top:1.7em;margin-bottom:1.7em>泛型：<strong style=font-size:inherit;line-height:inherit;color:#e96900>把类型明确的工作推迟到创建对象或调用方法的时候才去明确的特殊的类型</strong><p style=font-size:inherit;color:inherit;line-height:inherit;margin-top:1.7em;margin-bottom:1.7em>参数化类型:<ul class=list-paddingleft-2><li><p><strong style=font-size:inherit;line-height:inherit;color:#e96900>把类型当作是参数一样传递</strong></p><li><p><strong style=font-size:inherit;line-height:inherit;color:#e96900><code style="font-size:inherit;line-height:inherit;word-wrap:break-word;padding:2px 4px;border-radius:4px;margin-right:2px;margin-left:2px;color:#f82375;background:#f8f8f8">&lt;数据类型&gt;</code> 只能是引用类型</strong></p></ul><p style=font-size:inherit;color:inherit;line-height:inherit;margin-top:1.7em;margin-bottom:1.7em>相关术语：<ul class=list-paddingleft-2><li><p><code style="font-size:inherit;line-height:inherit;word-wrap:break-word;padding:2px 4px;border-radius:4px;margin-right:2px;margin-left:2px;color:#f82375;background:#f8f8f8">ArrayList&lt;E&gt;</code>中的<strong style=font-size:inherit;line-height:inherit;color:#e96900>E称为类型参数变量</strong></p><li><p><code style="font-size:inherit;line-height:inherit;word-wrap:break-word;padding:2px 4px;border-radius:4px;margin-right:2px;margin-left:2px;color:#f82375;background:#f8f8f8">ArrayList&lt;Integer&gt;</code>中的<strong style=font-size:inherit;line-height:inherit;color:#e96900>Integer称为实际类型参数</strong></p><li><p><strong style=font-size:inherit;line-height:inherit;color:#e96900>整个称为<code style="font-size:inherit;line-height:inherit;word-wrap:break-word;padding:2px 4px;border-radius:4px;margin-right:2px;margin-left:2px;color:#f82375;background:#f8f8f8">ArrayList&lt;E&gt;</code>泛型类型</strong></p><li><p><strong style=font-size:inherit;line-height:inherit;color:#e96900>整个<code style="font-size:inherit;line-height:inherit;word-wrap:break-word;padding:2px 4px;border-radius:4px;margin-right:2px;margin-left:2px;color:#f82375;background:#f8f8f8">ArrayList&lt;Integer&gt;</code>称为参数化的类型ParameterizedType</strong></p></ul><h1 style="color:inherit;line-height:inherit;margin-top:1.6em;margin-bottom:1.6em;font-weight:bold;font-size:1.6em;border-top:2px solid #039be5"><span style="font-size:inherit;line-height:inherit;display:inline-block;font-weight:normal;background:#039be5;color:#ffffff;padding-top:3px;padding-right:10px;padding-left:10px;border-bottom-right-radius:3px;border-bottom-left-radius:4px;border-bottom:2px solid #006064;border-right:3px solid #006064;margin-left:5px;margin-right:2px">二、为什么需要泛型</span></h1><p style=font-size:inherit;color:inherit;line-height:inherit;margin-top:1.7em;margin-bottom:1.7em><strong style=font-size:inherit;line-height:inherit;color:#e96900>早期Java是使用Object来代表任意类型的，但是向下转型有强转的问题，这样程序就不太安全</strong><p style=font-size:inherit;color:inherit;line-height:inherit;margin-top:1.7em;margin-bottom:1.7em>首先，我们来试想一下：没有泛型，集合会怎么样<ul class=list-paddingleft-2><li><p>Collection、Map集合对元素的类型是没有任何限制的。<strong style=font-size:inherit;line-height:inherit;color:#e96900>本来我的Collection集合装载的是全部的Dog对象，但是外边把Cat对象存储到集合中，是没有任何语法错误的。</strong></p><li><p>把对象扔进集合中，集合是不知道元素的类型是什么的，仅仅知道是Object。因此在get()的时候，返回的是Object。<strong style=font-size:inherit;line-height:inherit;color:#e96900>外边获取该对象，还需要强制转换</strong></p></ul><p style=font-size:inherit;color:inherit;line-height:inherit;margin-top:1.7em;margin-bottom:1.7em>有了泛型以后：<ul class=list-paddingleft-2><li><p><span style=font-size:inherit;color:inherit;line-height:inherit>代码更加简洁【不用强制转换】</span></p><li><p><span style=font-size:inherit;color:inherit;line-height:inherit>程序更加健壮【只要编译时期没有警告，那么运行时期就不会出现ClassCastException异常】</span></p><li><p><span style=font-size:inherit;color:inherit;line-height:inherit>可读性和稳定性【在编写集合的时候，就限定了类型】</span></p></ul><h2 style="color:inherit;line-height:inherit;margin-top:1.6em;margin-bottom:1.6em;font-weight:bold;font-size:1.4em;border-bottom:2px solid #00acc1"><span style=font-size:inherit;line-height:inherit;display:inline-block;font-weight:normal;background:#00acc1;color:#ffffff;padding-top:3px;padding-right:10px;padding-left:10px;border-top-right-radius:3px;border-top-left-radius:4px;margin-right:2px>2.1有了泛型后使用增强for遍历集合</span></h2><p style=font-size:inherit;color:inherit;line-height:inherit;margin-top:1.7em;margin-bottom:1.7em>在创建集合的时候，<strong style=font-size:inherit;line-height:inherit;color:#e96900>我们明确了集合的类型了</strong>，所以我们可以使用增强for来遍历集合！<pre style=font-size:inherit;color:inherit;line-height:inherit;margin-top:0px;margin-bottom:0px;padding:0px><code style=margin-right:2px;margin-left:2px;line-height:15px;font-size:11px;word-spacing:-3px;letter-spacing:0px;font-family:Consolas,Inconsolata,Courier,monospace;border-radius:0px;color:#a9b7c6;background:#282b2e;padding:.5em;display:block!important;word-wrap:normal!important;word-break:normal!important;overflow:auto!important> &nbsp; &nbsp; &nbsp; &nbsp;<span style=font-size:inherit;line-height:inherit;color:gray;word-wrap:inherit!important;word-break:inherit!important>//创建集合对象</span><br> &nbsp; &nbsp; &nbsp; &nbsp;ArrayList&lt;String&gt; list = <span style=font-size:inherit;line-height:inherit;color:#f82375;word-wrap:inherit!important;word-break:inherit!important>new</span> ArrayList&lt;&gt;();<br><br> &nbsp; &nbsp; &nbsp; &nbsp;list.add(<span style=font-size:inherit;line-height:inherit;color:#eedc70;word-wrap:inherit!important;word-break:inherit!important>"hello"</span>);<br> &nbsp; &nbsp; &nbsp; &nbsp;list.add(<span style=font-size:inherit;line-height:inherit;color:#eedc70;word-wrap:inherit!important;word-break:inherit!important>"world"</span>);<br> &nbsp; &nbsp; &nbsp; &nbsp;list.add(<span style=font-size:inherit;line-height:inherit;color:#eedc70;word-wrap:inherit!important;word-break:inherit!important>"java"</span>);<br><br> &nbsp; &nbsp; &nbsp; &nbsp;<span style=font-size:inherit;line-height:inherit;color:gray;word-wrap:inherit!important;word-break:inherit!important>//遍历,由于明确了类型.我们可以增强for</span><br> &nbsp; &nbsp; &nbsp; &nbsp;<span style=font-size:inherit;line-height:inherit;color:#f82375;word-wrap:inherit!important;word-break:inherit!important>for</span> (String s : list) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(s);<br> &nbsp; &nbsp; &nbsp; &nbsp;}<br></code></pre><h1 style="color:inherit;line-height:inherit;margin-top:1.6em;margin-bottom:1.6em;font-weight:bold;font-size:1.6em;border-top:2px solid #039be5"><span style="font-size:inherit;line-height:inherit;display:inline-block;font-weight:normal;background:#039be5;color:#ffffff;padding-top:3px;padding-right:10px;padding-left:10px;border-bottom-right-radius:3px;border-bottom-left-radius:4px;border-bottom:2px solid #006064;border-right:3px solid #006064;margin-left:5px;margin-right:2px">三、泛型基础</span></h1><h2 style="color:inherit;line-height:inherit;margin-top:1.6em;margin-bottom:1.6em;font-weight:bold;font-size:1.4em;border-bottom:2px solid #00acc1"><span style=font-size:inherit;line-height:inherit;display:inline-block;font-weight:normal;background:#00acc1;color:#ffffff;padding-top:3px;padding-right:10px;padding-left:10px;border-top-right-radius:3px;border-top-left-radius:4px;margin-right:2px>3.1泛型类</span></h2><p style=font-size:inherit;color:inherit;line-height:inherit;margin-top:1.7em;margin-bottom:1.7em><strong style=font-size:inherit;line-height:inherit;color:#e96900>泛型类就是把泛型定义在类上，用户使用该类的时候，才把类型明确下来</strong>….这样的话，用户明确了什么类型，该类就代表着什么类型…用户在使用的时候就不用担心强转的问题，运行时转换异常的问题了。<ul class=list-paddingleft-2><li><p><strong style=font-size:inherit;line-height:inherit;color:#e96900>在类上定义的泛型，在类的方法中也可以使用！</strong></p></ul><pre style=font-size:inherit;color:inherit;line-height:inherit;margin-top:0px;margin-bottom:0px;padding:0px><code style=margin-right:2px;margin-left:2px;line-height:15px;font-size:11px;word-spacing:-3px;letter-spacing:0px;font-family:Consolas,Inconsolata,Courier,monospace;border-radius:0px;color:#a9b7c6;background:#282b2e;padding:.5em;display:block!important;word-wrap:normal!important;word-break:normal!important;overflow:auto!important><span style=font-size:inherit;line-height:inherit;color:gray;word-wrap:inherit!important;word-break:inherit!important>/*<br> &nbsp; &nbsp;1:把泛型定义在类上<br> &nbsp; &nbsp;2:类型变量定义在类上,方法中也可以使用<br> */</span><br><span style=font-size:inherit;line-height:inherit;color:#f82375;word-wrap:inherit!important;word-break:inherit!important>public</span> <span style=font-size:inherit;color:inherit;line-height:inherit;word-wrap:inherit!important;word-break:inherit!important><span style=font-size:inherit;line-height:inherit;color:#f82375;word-wrap:inherit!important;word-break:inherit!important>class</span> <span style=font-size:inherit;line-height:inherit;color:#a5da2d;word-wrap:inherit!important;word-break:inherit!important>ObjectTool</span>&lt;<span style=font-size:inherit;line-height:inherit;color:#a5da2d;word-wrap:inherit!important;word-break:inherit!important>T</span>&gt; </span>{<br> &nbsp; &nbsp;<span style=font-size:inherit;line-height:inherit;color:#f82375;word-wrap:inherit!important;word-break:inherit!important>private</span> T obj;<br><br> &nbsp; &nbsp;<span style=font-size:inherit;line-height:inherit;color:#f82375;word-wrap:inherit!important;word-break:inherit!important><span style=font-size:inherit;line-height:inherit;word-wrap:inherit!important;word-break:inherit!important>public</span> T <span style=font-size:inherit;line-height:inherit;color:#a5da2d;word-wrap:inherit!important;word-break:inherit!important>getObj</span><span style=font-size:inherit;line-height:inherit;color:#ff9823;word-wrap:inherit!important;word-break:inherit!important>()</span> </span>{<br> &nbsp; &nbsp; &nbsp; &nbsp;<span style=font-size:inherit;line-height:inherit;color:#f82375;word-wrap:inherit!important;word-break:inherit!important>return</span> obj;<br> &nbsp; &nbsp;}<br><br> &nbsp; &nbsp;<span style=font-size:inherit;line-height:inherit;color:#f82375;word-wrap:inherit!important;word-break:inherit!important><span style=font-size:inherit;line-height:inherit;word-wrap:inherit!important;word-break:inherit!important>public</span> <span style=font-size:inherit;line-height:inherit;word-wrap:inherit!important;word-break:inherit!important>void</span> <span style=font-size:inherit;line-height:inherit;color:#a5da2d;word-wrap:inherit!important;word-break:inherit!important>setObj</span><span style=font-size:inherit;line-height:inherit;color:#ff9823;word-wrap:inherit!important;word-break:inherit!important>(T obj)</span> </span>{<br> &nbsp; &nbsp; &nbsp; &nbsp;<span style=font-size:inherit;line-height:inherit;color:#f82375;word-wrap:inherit!important;word-break:inherit!important>this</span>.obj = obj;<br> &nbsp; &nbsp;}<br>}<br></code></pre><ul class=list-paddingleft-2><li><p><span style=font-size:inherit;color:inherit;line-height:inherit>测试代码：</span></p></ul><p style=font-size:inherit;color:inherit;line-height:inherit;margin-top:1.7em;margin-bottom:1.7em><strong style=font-size:inherit;line-height:inherit;color:#e96900>用户想要使用哪种类型，就在创建的时候指定类型。使用的时候，该类就会自动转换成用户想要使用的类型了。</strong><pre style=font-size:inherit;color:inherit;line-height:inherit;margin-top:0px;margin-bottom:0px;padding:0px><code style=margin-right:2px;margin-left:2px;line-height:15px;font-size:11px;word-spacing:-3px;letter-spacing:0px;font-family:Consolas,Inconsolata,Courier,monospace;border-radius:0px;color:#a9b7c6;background:#282b2e;padding:.5em;display:block!important;word-wrap:normal!important;word-break:normal!important;overflow:auto!important> &nbsp; &nbsp;<span style=font-size:inherit;line-height:inherit;color:#f82375;word-wrap:inherit!important;word-break:inherit!important><span style=font-size:inherit;line-height:inherit;word-wrap:inherit!important;word-break:inherit!important>public</span> <span style=font-size:inherit;line-height:inherit;word-wrap:inherit!important;word-break:inherit!important>static</span> <span style=font-size:inherit;line-height:inherit;word-wrap:inherit!important;word-break:inherit!important>void</span> <span style=font-size:inherit;line-height:inherit;color:#a5da2d;word-wrap:inherit!important;word-break:inherit!important>main</span><span style=font-size:inherit;line-height:inherit;color:#ff9823;word-wrap:inherit!important;word-break:inherit!important>(String[] args)</span> </span>{<br> &nbsp; &nbsp; &nbsp; &nbsp;<span style=font-size:inherit;line-height:inherit;color:gray;word-wrap:inherit!important;word-break:inherit!important>//创建对象并指定元素类型</span><br> &nbsp; &nbsp; &nbsp; &nbsp;ObjectTool&lt;String&gt; tool = <span style=font-size:inherit;line-height:inherit;color:#f82375;word-wrap:inherit!important;word-break:inherit!important>new</span> ObjectTool&lt;&gt;();<br><br> &nbsp; &nbsp; &nbsp; &nbsp;tool.setObj(<span style=font-size:inherit;line-height:inherit;color:#f82375;word-wrap:inherit!important;word-break:inherit!important>new</span> String(<span style=font-size:inherit;line-height:inherit;color:#eedc70;word-wrap:inherit!important;word-break:inherit!important>"钟福成"</span>));<br> &nbsp; &nbsp; &nbsp; &nbsp;String s = tool.getObj();<br> &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(s);<br><br><br> &nbsp; &nbsp; &nbsp; &nbsp;<span style=font-size:inherit;line-height:inherit;color:gray;word-wrap:inherit!important;word-break:inherit!important>//创建对象并指定元素类型</span><br> &nbsp; &nbsp; &nbsp; &nbsp;ObjectTool&lt;Integer&gt; objectTool = <span style=font-size:inherit;line-height:inherit;color:#f82375;word-wrap:inherit!important;word-break:inherit!important>new</span> ObjectTool&lt;&gt;();<br> &nbsp; &nbsp; &nbsp; &nbsp;<span style=font-size:inherit;line-height:inherit;color:gray;word-wrap:inherit!important;word-break:inherit!important>/**<br> &nbsp; &nbsp; &nbsp; &nbsp; * 如果我在这个对象里传入的是String类型的,它在编译时期就通过不了了.<br> &nbsp; &nbsp; &nbsp; &nbsp; */</span><br> &nbsp; &nbsp; &nbsp; &nbsp;objectTool.setObj(<span style=font-size:inherit;line-height:inherit;color:#ae87fa;word-wrap:inherit!important;word-break:inherit!important>10</span>);<br> &nbsp; &nbsp; &nbsp; &nbsp;<span style=font-size:inherit;line-height:inherit;color:#f82375;word-wrap:inherit!important;word-break:inherit!important>int</span> i = objectTool.getObj();<br> &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(i);<br> &nbsp; &nbsp;}<br></code></pre><h2 style="color:inherit;line-height:inherit;margin-top:1.6em;margin-bottom:1.6em;font-weight:bold;font-size:1.4em;border-bottom:2px solid #00acc1"><span style=font-size:inherit;line-height:inherit;display:inline-block;font-weight:normal;background:#00acc1;color:#ffffff;padding-top:3px;padding-right:10px;padding-left:10px;border-top-right-radius:3px;border-top-left-radius:4px;margin-right:2px>3.2泛型方法</span></h2><p style=font-size:inherit;color:inherit;line-height:inherit;margin-top:1.7em;margin-bottom:1.7em>前面已经介绍了泛型类了，在类上定义的泛型，在方法中也可以使用…..<p style=font-size:inherit;color:inherit;line-height:inherit;margin-top:1.7em;margin-bottom:1.7em>现在呢，我们可能就仅仅在<strong style=font-size:inherit;line-height:inherit;color:#e96900>某一个方法上需要使用泛型</strong>….<strong style=font-size:inherit;line-height:inherit;color:#e96900>外界仅仅是关心该方法，不关心类其他的属性</strong>…这样的话，我们在整个类上定义泛型，未免就有些大题小作了。<ul class=list-paddingleft-2><li><p>定义泛型方法….<strong style=font-size:inherit;line-height:inherit;color:#e96900>泛型是先定义后使用的</strong></p></ul><pre style=font-size:inherit;color:inherit;line-height:inherit;margin-top:0px;margin-bottom:0px;padding:0px><code style=margin-right:2px;margin-left:2px;line-height:15px;font-size:11px;word-spacing:-3px;letter-spacing:0px;font-family:Consolas,Inconsolata,Courier,monospace;border-radius:0px;color:#a9b7c6;background:#282b2e;padding:.5em;display:block!important;word-wrap:normal!important;word-break:normal!important;overflow:auto!important> &nbsp; &nbsp;<span style=font-size:inherit;line-height:inherit;color:gray;word-wrap:inherit!important;word-break:inherit!important>//定义泛型方法..</span><br> &nbsp; &nbsp;<span style=font-size:inherit;line-height:inherit;color:#f82375;word-wrap:inherit!important;word-break:inherit!important>public</span> &lt;T&gt; <span style=font-size:inherit;line-height:inherit;color:#f82375;word-wrap:inherit!important;word-break:inherit!important><span style=font-size:inherit;line-height:inherit;word-wrap:inherit!important;word-break:inherit!important>void</span> <span style=font-size:inherit;line-height:inherit;color:#a5da2d;word-wrap:inherit!important;word-break:inherit!important>show</span><span style=font-size:inherit;line-height:inherit;color:#ff9823;word-wrap:inherit!important;word-break:inherit!important>(T t)</span> </span>{<br> &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(t);<br><br> &nbsp; &nbsp;}<br></code></pre><ul class=list-paddingleft-2><li><p><span style=font-size:inherit;color:inherit;line-height:inherit>测试代码：</span></p></ul><p style=font-size:inherit;color:inherit;line-height:inherit;margin-top:1.7em;margin-bottom:1.7em><strong style=font-size:inherit;line-height:inherit;color:#e96900>用户传递进来的是什么类型，返回值就是什么类型了</strong><pre style=font-size:inherit;color:inherit;line-height:inherit;margin-top:0px;margin-bottom:0px;padding:0px><code style=margin-right:2px;margin-left:2px;line-height:15px;font-size:11px;word-spacing:-3px;letter-spacing:0px;font-family:Consolas,Inconsolata,Courier,monospace;border-radius:0px;color:#a9b7c6;background:#282b2e;padding:.5em;display:block!important;word-wrap:normal!important;word-break:normal!important;overflow:auto!important> &nbsp; &nbsp;<span style=font-size:inherit;line-height:inherit;color:#f82375;word-wrap:inherit!important;word-break:inherit!important><span style=font-size:inherit;line-height:inherit;word-wrap:inherit!important;word-break:inherit!important>public</span> <span style=font-size:inherit;line-height:inherit;word-wrap:inherit!important;word-break:inherit!important>static</span> <span style=font-size:inherit;line-height:inherit;word-wrap:inherit!important;word-break:inherit!important>void</span> <span style=font-size:inherit;line-height:inherit;color:#a5da2d;word-wrap:inherit!important;word-break:inherit!important>main</span><span style=font-size:inherit;line-height:inherit;color:#ff9823;word-wrap:inherit!important;word-break:inherit!important>(String[] args)</span> </span>{<br> &nbsp; &nbsp; &nbsp; &nbsp;<span style=font-size:inherit;line-height:inherit;color:gray;word-wrap:inherit!important;word-break:inherit!important>//创建对象</span><br> &nbsp; &nbsp; &nbsp; &nbsp;ObjectTool tool = <span style=font-size:inherit;line-height:inherit;color:#f82375;word-wrap:inherit!important;word-break:inherit!important>new</span> ObjectTool();<br><br> &nbsp; &nbsp; &nbsp; &nbsp;<span style=font-size:inherit;line-height:inherit;color:gray;word-wrap:inherit!important;word-break:inherit!important>//调用方法,传入的参数是什么类型,返回值就是什么类型</span><br> &nbsp; &nbsp; &nbsp; &nbsp;tool.show(<span style=font-size:inherit;line-height:inherit;color:#eedc70;word-wrap:inherit!important;word-break:inherit!important>"hello"</span>);<br> &nbsp; &nbsp; &nbsp; &nbsp;tool.show(<span style=font-size:inherit;line-height:inherit;color:#ae87fa;word-wrap:inherit!important;word-break:inherit!important>12</span>);<br> &nbsp; &nbsp; &nbsp; &nbsp;tool.show(<span style=font-size:inherit;line-height:inherit;color:#ae87fa;word-wrap:inherit!important;word-break:inherit!important>12.5</span>);<br><br> &nbsp; &nbsp;}<br></code></pre><h2 style="color:inherit;line-height:inherit;margin-top:1.6em;margin-bottom:1.6em;font-weight:bold;font-size:1.4em;border-bottom:2px solid #00acc1"><span style=font-size:inherit;line-height:inherit;display:inline-block;font-weight:normal;background:#00acc1;color:#ffffff;padding-top:3px;padding-right:10px;padding-left:10px;border-top-right-radius:3px;border-top-left-radius:4px;margin-right:2px>3.3泛型类派生出的子类</span></h2><p style=font-size:inherit;color:inherit;line-height:inherit;margin-top:1.7em;margin-bottom:1.7em>前面我们已经定义了泛型类，<strong style=font-size:inherit;line-height:inherit;color:#e96900>泛型类是拥有泛型这个特性的类，它本质上还是一个Java类，那么它就可以被继承</strong><p style=font-size:inherit;color:inherit;line-height:inherit;margin-top:1.7em;margin-bottom:1.7em>那它是怎么被继承的呢？？这里分两种情况<ol class=list-paddingleft-2><li><p><strong style=font-size:inherit;line-height:inherit;color:#e96900>子类明确泛型类的类型参数变量</strong></p><li><p><strong style=font-size:inherit;line-height:inherit;color:#e96900>子类不明确泛型类的类型参数变量</strong></p></ol><h3 style="color:inherit;line-height:inherit;margin-top:1.6em;margin-bottom:1.6em;font-weight:bold;border-bottom:2px solid #ef7060;font-size:1.3em"><span style="font-size:inherit;line-height:inherit;display:inline-block;font-weight:normal;background:#ef7060;color:#ffffff;padding:3px 10px 1px;border-top-right-radius:3px;border-top-left-radius:3px;margin-right:3px">3.3.1子类明确泛型类的类型参数变量</span><span style="display:inline-block;vertical-align:bottom;border-bottom:36px solid #efebe9;border-right:20px solid transparent"> </span></h3><ul class=list-paddingleft-2><li><p><span style=font-size:inherit;color:inherit;line-height:inherit>泛型接口</span></p></ul><pre style=font-size:inherit;color:inherit;line-height:inherit;margin-top:0px;margin-bottom:0px;padding:0px><code style=margin-right:2px;margin-left:2px;line-height:15px;font-size:11px;word-spacing:-3px;letter-spacing:0px;font-family:Consolas,Inconsolata,Courier,monospace;border-radius:0px;color:#a9b7c6;background:#282b2e;padding:.5em;display:block!important;word-wrap:normal!important;word-break:normal!important;overflow:auto!important><span style=font-size:inherit;line-height:inherit;color:gray;word-wrap:inherit!important;word-break:inherit!important>/*<br> &nbsp; &nbsp;把泛型定义在接口上<br> */</span><br><span style=font-size:inherit;line-height:inherit;color:#f82375;word-wrap:inherit!important;word-break:inherit!important>public</span> <span style=font-size:inherit;color:inherit;line-height:inherit;word-wrap:inherit!important;word-break:inherit!important><span style=font-size:inherit;line-height:inherit;color:#f82375;word-wrap:inherit!important;word-break:inherit!important>interface</span> <span style=font-size:inherit;line-height:inherit;color:#a5da2d;word-wrap:inherit!important;word-break:inherit!important>Inter</span>&lt;<span style=font-size:inherit;line-height:inherit;color:#a5da2d;word-wrap:inherit!important;word-break:inherit!important>T</span>&gt; </span>{<br> &nbsp; &nbsp;<span style=font-size:inherit;line-height:inherit;color:#f82375;word-wrap:inherit!important;word-break:inherit!important><span style=font-size:inherit;line-height:inherit;word-wrap:inherit!important;word-break:inherit!important>public</span> <span style=font-size:inherit;line-height:inherit;word-wrap:inherit!important;word-break:inherit!important>abstract</span> <span style=font-size:inherit;line-height:inherit;word-wrap:inherit!important;word-break:inherit!important>void</span> <span style=font-size:inherit;line-height:inherit;color:#a5da2d;word-wrap:inherit!important;word-break:inherit!important>show</span><span style=font-size:inherit;line-height:inherit;color:#ff9823;word-wrap:inherit!important;word-break:inherit!important>(T t)</span></span>;<br><br>}<br></code></pre><ul class=list-paddingleft-2><li><p><span style=font-size:inherit;color:inherit;line-height:inherit>实现泛型接口的类…..</span></p></ul><pre style=font-size:inherit;color:inherit;line-height:inherit;margin-top:0px;margin-bottom:0px;padding:0px><code style=margin-right:2px;margin-left:2px;line-height:15px;font-size:11px;word-spacing:-3px;letter-spacing:0px;font-family:Consolas,Inconsolata,Courier,monospace;border-radius:0px;color:#a9b7c6;background:#282b2e;padding:.5em;display:block!important;word-wrap:normal!important;word-break:normal!important;overflow:auto!important><span style=font-size:inherit;line-height:inherit;color:gray;word-wrap:inherit!important;word-break:inherit!important>/**<br> * 子类明确泛型类的类型参数变量:<br> */</span><br><br><span style=font-size:inherit;line-height:inherit;color:#f82375;word-wrap:inherit!important;word-break:inherit!important>public</span> <span style=font-size:inherit;color:inherit;line-height:inherit;word-wrap:inherit!important;word-break:inherit!important><span style=font-size:inherit;line-height:inherit;color:#f82375;word-wrap:inherit!important;word-break:inherit!important>class</span> <span style=font-size:inherit;line-height:inherit;color:#a5da2d;word-wrap:inherit!important;word-break:inherit!important>InterImpl</span> <span style=font-size:inherit;line-height:inherit;color:#f82375;word-wrap:inherit!important;word-break:inherit!important>implements</span> <span style=font-size:inherit;line-height:inherit;color:#a5da2d;word-wrap:inherit!important;word-break:inherit!important>Inter</span>&lt;<span style=font-size:inherit;line-height:inherit;color:#a5da2d;word-wrap:inherit!important;word-break:inherit!important>String</span>&gt; </span>{<br> &nbsp; &nbsp;<span style=font-size:inherit;line-height:inherit;color:#5bdaed;word-wrap:inherit!important;word-break:inherit!important>@Override</span><br> &nbsp; &nbsp;<span style=font-size:inherit;line-height:inherit;color:#f82375;word-wrap:inherit!important;word-break:inherit!important><span style=font-size:inherit;line-height:inherit;word-wrap:inherit!important;word-break:inherit!important>public</span> <span style=font-size:inherit;line-height:inherit;word-wrap:inherit!important;word-break:inherit!important>void</span> <span style=font-size:inherit;line-height:inherit;color:#a5da2d;word-wrap:inherit!important;word-break:inherit!important>show</span><span style=font-size:inherit;line-height:inherit;color:#ff9823;word-wrap:inherit!important;word-break:inherit!important>(String s)</span> </span>{<br> &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(s);<br><br> &nbsp; &nbsp;}<br>}<br></code></pre><h3 style="color:inherit;line-height:inherit;margin-top:1.6em;margin-bottom:1.6em;font-weight:bold;border-bottom:2px solid #ef7060;font-size:1.3em"><span style="font-size:inherit;line-height:inherit;display:inline-block;font-weight:normal;background:#ef7060;color:#ffffff;padding:3px 10px 1px;border-top-right-radius:3px;border-top-left-radius:3px;margin-right:3px">3.3.2子类不明确泛型类的类型参数变量</span><span style="display:inline-block;vertical-align:bottom;border-bottom:36px solid #efebe9;border-right:20px solid transparent"> </span></h3><ul class=list-paddingleft-2><li><p>当子类不明确泛型类的类型参数变量时，<strong style=font-size:inherit;line-height:inherit;color:#e96900>外界使用子类的时候，也需要传递类型参数变量进来，在实现类上需要定义出类型参数变量</strong></p></ul><pre style=font-size:inherit;color:inherit;line-height:inherit;margin-top:0px;margin-bottom:0px;padding:0px><code style=margin-right:2px;margin-left:2px;line-height:15px;font-size:11px;word-spacing:-3px;letter-spacing:0px;font-family:Consolas,Inconsolata,Courier,monospace;border-radius:0px;color:#a9b7c6;background:#282b2e;padding:.5em;display:block!important;word-wrap:normal!important;word-break:normal!important;overflow:auto!important><span style=font-size:inherit;line-height:inherit;color:gray;word-wrap:inherit!important;word-break:inherit!important>/**<br> * 子类不明确泛型类的类型参数变量:<br> * &nbsp; &nbsp; &nbsp;实现类也要定义出&lt;T&gt;类型的<br> *<br> */</span><br><span style=font-size:inherit;line-height:inherit;color:#f82375;word-wrap:inherit!important;word-break:inherit!important>public</span> <span style=font-size:inherit;color:inherit;line-height:inherit;word-wrap:inherit!important;word-break:inherit!important><span style=font-size:inherit;line-height:inherit;color:#f82375;word-wrap:inherit!important;word-break:inherit!important>class</span> <span style=font-size:inherit;line-height:inherit;color:#a5da2d;word-wrap:inherit!important;word-break:inherit!important>InterImpl</span>&lt;<span style=font-size:inherit;line-height:inherit;color:#a5da2d;word-wrap:inherit!important;word-break:inherit!important>T</span>&gt; <span style=font-size:inherit;line-height:inherit;color:#f82375;word-wrap:inherit!important;word-break:inherit!important>implements</span> <span style=font-size:inherit;line-height:inherit;color:#a5da2d;word-wrap:inherit!important;word-break:inherit!important>Inter</span>&lt;<span style=font-size:inherit;line-height:inherit;color:#a5da2d;word-wrap:inherit!important;word-break:inherit!important>T</span>&gt; </span>{<br><br> &nbsp; &nbsp;<span style=font-size:inherit;line-height:inherit;color:#5bdaed;word-wrap:inherit!important;word-break:inherit!important>@Override</span><br> &nbsp; &nbsp;<span style=font-size:inherit;line-height:inherit;color:#f82375;word-wrap:inherit!important;word-break:inherit!important><span style=font-size:inherit;line-height:inherit;word-wrap:inherit!important;word-break:inherit!important>public</span> <span style=font-size:inherit;line-height:inherit;word-wrap:inherit!important;word-break:inherit!important>void</span> <span style=font-size:inherit;line-height:inherit;color:#a5da2d;word-wrap:inherit!important;word-break:inherit!important>show</span><span style=font-size:inherit;line-height:inherit;color:#ff9823;word-wrap:inherit!important;word-break:inherit!important>(T t)</span> </span>{<br> &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(t);<br><br> &nbsp; &nbsp;}<br>}<br></code></pre><p style=font-size:inherit;color:inherit;line-height:inherit;margin-top:1.7em;margin-bottom:1.7em><strong style=font-size:inherit;line-height:inherit;color:#e96900>测试代码:</strong><pre style=font-size:inherit;color:inherit;line-height:inherit;margin-top:0px;margin-bottom:0px;padding:0px><code style=margin-right:2px;margin-left:2px;line-height:15px;font-size:11px;word-spacing:-3px;letter-spacing:0px;font-family:Consolas,Inconsolata,Courier,monospace;border-radius:0px;color:#a9b7c6;background:#282b2e;padding:.5em;display:block!important;word-wrap:normal!important;word-break:normal!important;overflow:auto!important> &nbsp; &nbsp;<span style=font-size:inherit;line-height:inherit;color:#f82375;word-wrap:inherit!important;word-break:inherit!important><span style=font-size:inherit;line-height:inherit;word-wrap:inherit!important;word-break:inherit!important>public</span> <span style=font-size:inherit;line-height:inherit;word-wrap:inherit!important;word-break:inherit!important>static</span> <span style=font-size:inherit;line-height:inherit;word-wrap:inherit!important;word-break:inherit!important>void</span> <span style=font-size:inherit;line-height:inherit;color:#a5da2d;word-wrap:inherit!important;word-break:inherit!important>main</span><span style=font-size:inherit;line-height:inherit;color:#ff9823;word-wrap:inherit!important;word-break:inherit!important>(String[] args)</span> </span>{<br> &nbsp; &nbsp; &nbsp; &nbsp;<span style=font-size:inherit;line-height:inherit;color:gray;word-wrap:inherit!important;word-break:inherit!important>//测试第一种情况</span><br> &nbsp; &nbsp; &nbsp; &nbsp;<span style=font-size:inherit;line-height:inherit;color:gray;word-wrap:inherit!important;word-break:inherit!important>//Inter&lt;String&gt; i = new InterImpl();</span><br> &nbsp; &nbsp; &nbsp; &nbsp;<span style=font-size:inherit;line-height:inherit;color:gray;word-wrap:inherit!important;word-break:inherit!important>//i.show("hello");</span><br><br> &nbsp; &nbsp; &nbsp; &nbsp;<span style=font-size:inherit;line-height:inherit;color:gray;word-wrap:inherit!important;word-break:inherit!important>//第二种情况测试</span><br> &nbsp; &nbsp; &nbsp; &nbsp;Inter&lt;String&gt; ii = <span style=font-size:inherit;line-height:inherit;color:#f82375;word-wrap:inherit!important;word-break:inherit!important>new</span> InterImpl&lt;&gt;();<br> &nbsp; &nbsp; &nbsp; &nbsp;ii.show(<span style=font-size:inherit;line-height:inherit;color:#eedc70;word-wrap:inherit!important;word-break:inherit!important>"100"</span>);<br><br> &nbsp; &nbsp;}<br></code></pre><p style=font-size:inherit;color:inherit;line-height:inherit;margin-top:1.7em;margin-bottom:1.7em>值得注意的是：<ul class=list-paddingleft-2><li><p><strong style=font-size:inherit;line-height:inherit;color:#e96900>实现类的要是重写父类的方法，返回值的类型是要和父类一样的！</strong></p><li><p><strong style=font-size:inherit;line-height:inherit;color:#e96900>类上声明的泛形只对非静态成员有效</strong></p></ul><hr style="font-size:inherit;color:inherit;line-height:inherit;height:8px;margin:2.5rem 20px;border-radius:50%;border-top:3px solid #a5a5a5"><h2 style="color:inherit;line-height:inherit;margin-top:1.6em;margin-bottom:1.6em;font-weight:bold;font-size:1.4em;border-bottom:2px solid #00acc1"><span style=font-size:inherit;line-height:inherit;display:inline-block;font-weight:normal;background:#00acc1;color:#ffffff;padding-top:3px;padding-right:10px;padding-left:10px;border-top-right-radius:3px;border-top-left-radius:4px;margin-right:2px>3.4类型通配符</span></h2><p style=font-size:inherit;color:inherit;line-height:inherit;margin-top:1.7em;margin-bottom:1.7em>为什么需要类型通配符？？？？我们来看一个需求…….<p style=font-size:inherit;color:inherit;line-height:inherit;margin-top:1.7em;margin-bottom:1.7em>现在有个需求：<strong style=font-size:inherit;line-height:inherit;color:#e96900>方法接收一个集合参数，遍历集合并把集合元素打印出来，怎么办？</strong><ul class=list-paddingleft-2><li><p><span style=font-size:inherit;color:inherit;line-height:inherit>按照我们没有学习泛型之前，我们可能会这样做：</span></p></ul><pre style=font-size:inherit;color:inherit;line-height:inherit;margin-top:0px;margin-bottom:0px;padding:0px><code style=margin-right:2px;margin-left:2px;line-height:15px;font-size:11px;word-spacing:-3px;letter-spacing:0px;font-family:Consolas,Inconsolata,Courier,monospace;border-radius:0px;color:#a9b7c6;background:#282b2e;padding:.5em;display:block!important;word-wrap:normal!important;word-break:normal!important;overflow:auto!important><span style=font-size:inherit;line-height:inherit;color:#f82375;word-wrap:inherit!important;word-break:inherit!important><span style=font-size:inherit;line-height:inherit;word-wrap:inherit!important;word-break:inherit!important>public</span> <span style=font-size:inherit;line-height:inherit;word-wrap:inherit!important;word-break:inherit!important>void</span> <span style=font-size:inherit;line-height:inherit;color:#a5da2d;word-wrap:inherit!important;word-break:inherit!important>test</span><span style=font-size:inherit;line-height:inherit;color:#ff9823;word-wrap:inherit!important;word-break:inherit!important>(List list)</span></span>{<br><br><br> &nbsp; &nbsp;<span style=font-size:inherit;line-height:inherit;color:#f82375;word-wrap:inherit!important;word-break:inherit!important>for</span>(<span style=font-size:inherit;line-height:inherit;color:#f82375;word-wrap:inherit!important;word-break:inherit!important>int</span> i=<span style=font-size:inherit;line-height:inherit;color:#ae87fa;word-wrap:inherit!important;word-break:inherit!important>0</span>;i&lt;list.size();i++){<br><br> &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(list.get(i));<br><br> &nbsp; &nbsp;}<br>}<br></code></pre><p style=font-size:inherit;color:inherit;line-height:inherit;margin-top:1.7em;margin-bottom:1.7em>上面的代码是正确的，<strong style=font-size:inherit;line-height:inherit;color:#e96900>只不过在编译的时候会出现警告，说没有确定集合元素的类型</strong>….这样是不优雅的…<ul class=list-paddingleft-2><li><p><span style=font-size:inherit;color:inherit;line-height:inherit>那我们学习了泛型了，现在要怎么做呢？？有的人可能会这样做：</span></p></ul><pre style=font-size:inherit;color:inherit;line-height:inherit;margin-top:0px;margin-bottom:0px;padding:0px><code style=margin-right:2px;margin-left:2px;line-height:15px;font-size:11px;word-spacing:-3px;letter-spacing:0px;font-family:Consolas,Inconsolata,Courier,monospace;border-radius:0px;color:#a9b7c6;background:#282b2e;padding:.5em;display:block!important;word-wrap:normal!important;word-break:normal!important;overflow:auto!important><span style=font-size:inherit;line-height:inherit;color:#f82375;word-wrap:inherit!important;word-break:inherit!important><span style=font-size:inherit;line-height:inherit;word-wrap:inherit!important;word-break:inherit!important>public</span> <span style=font-size:inherit;line-height:inherit;word-wrap:inherit!important;word-break:inherit!important>void</span> <span style=font-size:inherit;line-height:inherit;color:#a5da2d;word-wrap:inherit!important;word-break:inherit!important>test</span><span style=font-size:inherit;line-height:inherit;color:#ff9823;word-wrap:inherit!important;word-break:inherit!important>(List&lt;Object&gt; list)</span></span>{<br><br><br> &nbsp; &nbsp;<span style=font-size:inherit;line-height:inherit;color:#f82375;word-wrap:inherit!important;word-break:inherit!important>for</span>(<span style=font-size:inherit;line-height:inherit;color:#f82375;word-wrap:inherit!important;word-break:inherit!important>int</span> i=<span style=font-size:inherit;line-height:inherit;color:#ae87fa;word-wrap:inherit!important;word-break:inherit!important>0</span>;i&lt;list.size();i++){<br><br> &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(list.get(i));<br><br> &nbsp; &nbsp;}<br>}<br></code></pre><p style=font-size:inherit;color:inherit;line-height:inherit;margin-top:1.7em;margin-bottom:1.7em>这样做语法是没毛病的，但是这里十分值得注意的是：<strong style=font-size:inherit;line-height:inherit;color:#e96900>该test()方法只能遍历装载着Object的集合！！！</strong><p style=font-size:inherit;color:inherit;line-height:inherit;margin-top:1.7em;margin-bottom:1.7em>强调：<strong style=font-size:inherit;line-height:inherit;color:#e96900>泛型中的<code style="font-size:inherit;line-height:inherit;word-wrap:break-word;padding:2px 4px;border-radius:4px;margin-right:2px;margin-left:2px;color:#f82375;background:#f8f8f8">&lt;Object&gt;</code>并不是像以前那样有继承关系的，也就是说<code style="font-size:inherit;line-height:inherit;word-wrap:break-word;padding:2px 4px;border-radius:4px;margin-right:2px;margin-left:2px;color:#f82375;background:#f8f8f8">List&lt;Object&gt;</code>和<code style="font-size:inherit;line-height:inherit;word-wrap:break-word;padding:2px 4px;border-radius:4px;margin-right:2px;margin-left:2px;color:#f82375;background:#f8f8f8">List&lt;String&gt;</code>是毫无关系的！！！！</strong><p style=font-size:inherit;color:inherit;line-height:inherit;margin-top:1.7em;margin-bottom:1.7em>那现在咋办？？？我们是不清楚List集合装载的元素是什么类型的，<code style="font-size:inherit;line-height:inherit;word-wrap:break-word;padding:2px 4px;border-radius:4px;margin-right:2px;margin-left:2px;color:#f82375;background:#f8f8f8">List&lt;Objcet&gt;</code>这样是行不通的……..<strong style=font-size:inherit;line-height:inherit;color:#e96900>于是Java泛型提供了类型通配符 &nbsp;?</strong><p style=font-size:inherit;color:inherit;line-height:inherit;margin-top:1.7em;margin-bottom:1.7em>所以代码应该改成这样：<pre style=font-size:inherit;color:inherit;line-height:inherit;margin-top:0px;margin-bottom:0px;padding:0px><code style=margin-right:2px;margin-left:2px;line-height:15px;font-size:11px;word-spacing:-3px;letter-spacing:0px;font-family:Consolas,Inconsolata,Courier,monospace;border-radius:0px;color:#a9b7c6;background:#282b2e;padding:.5em;display:block!important;word-wrap:normal!important;word-break:normal!important;overflow:auto!important><span style=font-size:inherit;line-height:inherit;color:#f82375;word-wrap:inherit!important;word-break:inherit!important><span style=font-size:inherit;line-height:inherit;word-wrap:inherit!important;word-break:inherit!important>public</span> <span style=font-size:inherit;line-height:inherit;word-wrap:inherit!important;word-break:inherit!important>void</span> <span style=font-size:inherit;line-height:inherit;color:#a5da2d;word-wrap:inherit!important;word-break:inherit!important>test</span><span style=font-size:inherit;line-height:inherit;color:#ff9823;word-wrap:inherit!important;word-break:inherit!important>(List&lt;?&gt; list)</span></span>{<br><br><br> &nbsp; &nbsp;<span style=font-size:inherit;line-height:inherit;color:#f82375;word-wrap:inherit!important;word-break:inherit!important>for</span>(<span style=font-size:inherit;line-height:inherit;color:#f82375;word-wrap:inherit!important;word-break:inherit!important>int</span> i=<span style=font-size:inherit;line-height:inherit;color:#ae87fa;word-wrap:inherit!important;word-break:inherit!important>0</span>;i&lt;list.size();i++){<br><br> &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(list.get(i));<br><br> &nbsp; &nbsp;}<br>}<br></code></pre><p style=font-size:inherit;color:inherit;line-height:inherit;margin-top:1.7em;margin-bottom:1.7em><strong style=font-size:inherit;line-height:inherit;color:#e96900>?号通配符表示可以匹配任意类型，任意的Java类都可以匹配</strong>…..<p style=font-size:inherit;color:inherit;line-height:inherit;margin-top:1.7em;margin-bottom:1.7em>现在非常值得注意的是，当我们使用?号通配符的时候：<strong style=font-size:inherit;line-height:inherit;color:#e96900>就只能调对象与类型无关的方法，不能调用对象与类型有关的方法。</strong><p style=font-size:inherit;color:inherit;line-height:inherit;margin-top:1.7em;margin-bottom:1.7em>记住，<strong style=font-size:inherit;line-height:inherit;color:#e96900>只能调用与对象无关的方法，不能调用对象与类型有关的方法</strong>。因为直到外界使用才知道具体的类型是什么。也就是说，在上面的List集合，我是不能使用add()方法的。<strong style=font-size:inherit;line-height:inherit;color:#e96900>因为add()方法是把对象丢进集合中，而现在我是不知道对象的类型是什么。</strong><hr style="font-size:inherit;color:inherit;line-height:inherit;height:8px;margin:2.5rem 20px;border-radius:50%;border-top:3px solid #a5a5a5"><h3 style="color:inherit;line-height:inherit;margin-top:1.6em;margin-bottom:1.6em;font-weight:bold;border-bottom:2px solid #ef7060;font-size:1.3em"><span style="font-size:inherit;line-height:inherit;display:inline-block;font-weight:normal;background:#ef7060;color:#ffffff;padding:3px 10px 1px;border-top-right-radius:3px;border-top-left-radius:3px;margin-right:3px">3.4.1设定通配符上限</span><span style="display:inline-block;vertical-align:bottom;border-bottom:36px solid #efebe9;border-right:20px solid transparent"> </span></h3><p style=font-size:inherit;color:inherit;line-height:inherit;margin-top:1.7em;margin-bottom:1.7em>首先，我们来看一下设定通配符上限用在哪里….<p style=font-size:inherit;color:inherit;line-height:inherit;margin-top:1.7em;margin-bottom:1.7em>现在，我想接收一个List集合，它只能操作数字类型的元素【Float、Integer、Double、Byte等数字类型都行】，怎么做？？？<p style=font-size:inherit;color:inherit;line-height:inherit;margin-top:1.7em;margin-bottom:1.7em>我们学习了通配符，但是如果直接使用通配符的话，该集合就不是只能操作数字了。因此我们需要<strong style=font-size:inherit;line-height:inherit;color:#e96900>用到设定通配符上限</strong><pre style=font-size:inherit;color:inherit;line-height:inherit;margin-top:0px;margin-bottom:0px;padding:0px><code style=margin-right:2px;margin-left:2px;line-height:15px;font-size:11px;word-spacing:-3px;letter-spacing:0px;font-family:Consolas,Inconsolata,Courier,monospace;border-radius:0px;color:#a9b7c6;background:#282b2e;padding:.5em;display:block!important;word-wrap:normal!important;word-break:normal!important;overflow:auto!important> &nbsp; &nbsp;List&lt;? extends Number&gt;<br></code></pre><p style=font-size:inherit;color:inherit;line-height:inherit;margin-top:1.7em;margin-bottom:1.7em>上面的代码表示的是：<strong style=font-size:inherit;line-height:inherit;color:#e96900>List集合装载的元素只能是Number的子类或自身</strong><pre style=font-size:inherit;color:inherit;line-height:inherit;margin-top:0px;margin-bottom:0px;padding:0px><code style=margin-right:2px;margin-left:2px;line-height:15px;font-size:11px;word-spacing:-3px;letter-spacing:0px;font-family:Consolas,Inconsolata,Courier,monospace;border-radius:0px;color:#a9b7c6;background:#282b2e;padding:.5em;display:block!important;word-wrap:normal!important;word-break:normal!important;overflow:auto!important> &nbsp; &nbsp;<span style=font-size:inherit;line-height:inherit;color:#f82375;word-wrap:inherit!important;word-break:inherit!important><span style=font-size:inherit;line-height:inherit;word-wrap:inherit!important;word-break:inherit!important>public</span> <span style=font-size:inherit;line-height:inherit;word-wrap:inherit!important;word-break:inherit!important>static</span> <span style=font-size:inherit;line-height:inherit;word-wrap:inherit!important;word-break:inherit!important>void</span> <span style=font-size:inherit;line-height:inherit;color:#a5da2d;word-wrap:inherit!important;word-break:inherit!important>main</span><span style=font-size:inherit;line-height:inherit;color:#ff9823;word-wrap:inherit!important;word-break:inherit!important>(String[] args)</span> </span>{<br><br><br> &nbsp; &nbsp; &nbsp; &nbsp;<span style=font-size:inherit;line-height:inherit;color:gray;word-wrap:inherit!important;word-break:inherit!important>//List集合装载的是Integer，可以调用该方法</span><br> &nbsp; &nbsp; &nbsp; &nbsp;List&lt;Integer&gt; integer = <span style=font-size:inherit;line-height:inherit;color:#f82375;word-wrap:inherit!important;word-break:inherit!important>new</span> ArrayList&lt;&gt;();<br> &nbsp; &nbsp; &nbsp; &nbsp;test(integer);<br><br> &nbsp; &nbsp; &nbsp; &nbsp;<span style=font-size:inherit;line-height:inherit;color:gray;word-wrap:inherit!important;word-break:inherit!important>//List集合装载的是String，在编译时期就报错了</span><br> &nbsp; &nbsp; &nbsp; &nbsp;List&lt;String&gt; strings = <span style=font-size:inherit;line-height:inherit;color:#f82375;word-wrap:inherit!important;word-break:inherit!important>new</span> ArrayList&lt;&gt;();<br> &nbsp; &nbsp; &nbsp; &nbsp;test(strings);<br><br> &nbsp; &nbsp;}<br><br><br> &nbsp; &nbsp;<span style=font-size:inherit;line-height:inherit;color:#f82375;word-wrap:inherit!important;word-break:inherit!important><span style=font-size:inherit;line-height:inherit;word-wrap:inherit!important;word-break:inherit!important>public</span> <span style=font-size:inherit;line-height:inherit;word-wrap:inherit!important;word-break:inherit!important>static</span> <span style=font-size:inherit;line-height:inherit;word-wrap:inherit!important;word-break:inherit!important>void</span> <span style=font-size:inherit;line-height:inherit;color:#a5da2d;word-wrap:inherit!important;word-break:inherit!important>test</span><span style=font-size:inherit;line-height:inherit;color:#ff9823;word-wrap:inherit!important;word-break:inherit!important>(List&lt;? extends Number&gt; list)</span> </span>{<br><br> &nbsp; &nbsp;}<br></code></pre><h3 style="color:inherit;line-height:inherit;margin-top:1.6em;margin-bottom:1.6em;font-weight:bold;border-bottom:2px solid #ef7060;font-size:1.3em"><span style="font-size:inherit;line-height:inherit;display:inline-block;font-weight:normal;background:#ef7060;color:#ffffff;padding:3px 10px 1px;border-top-right-radius:3px;border-top-left-radius:3px;margin-right:3px">3.4.2设定通配符下限</span><span style="display:inline-block;vertical-align:bottom;border-bottom:36px solid #efebe9;border-right:20px solid transparent"> </span></h3><p style=font-size:inherit;color:inherit;line-height:inherit;margin-top:1.7em;margin-bottom:1.7em>既然上面我们已经说了如何设定通配符的上限，那么设定通配符的下限也不是陌生的事了。直接来看语法吧<pre style=font-size:inherit;color:inherit;line-height:inherit;margin-top:0px;margin-bottom:0px;padding:0px><code style=margin-right:2px;margin-left:2px;line-height:15px;font-size:11px;word-spacing:-3px;letter-spacing:0px;font-family:Consolas,Inconsolata,Courier,monospace;border-radius:0px;color:#a9b7c6;background:#282b2e;padding:.5em;display:block!important;word-wrap:normal!important;word-break:normal!important;overflow:auto!important> &nbsp; &nbsp;<span style=font-size:inherit;line-height:inherit;color:gray;word-wrap:inherit!important;word-break:inherit!important>//传递进来的只能是Type或Type的父类</span><br> &nbsp; &nbsp;&lt;? <span style=font-size:inherit;line-height:inherit;color:#f82375;word-wrap:inherit!important;word-break:inherit!important>super</span> Type&gt;<br></code></pre><p style=font-size:inherit;color:inherit;line-height:inherit;margin-top:1.7em;margin-bottom:1.7em>设定通配符的下限这并不少见，在TreeSet集合中就有….我们来看一下<pre style=font-size:inherit;color:inherit;line-height:inherit;margin-top:0px;margin-bottom:0px;padding:0px><code style=margin-right:2px;margin-left:2px;line-height:15px;font-size:11px;word-spacing:-3px;letter-spacing:0px;font-family:Consolas,Inconsolata,Courier,monospace;border-radius:0px;color:#a9b7c6;background:#282b2e;padding:.5em;display:block!important;word-wrap:normal!important;word-break:normal!important;overflow:auto!important> &nbsp; &nbsp;<span style=font-size:inherit;line-height:inherit;color:#f82375;word-wrap:inherit!important;word-break:inherit!important><span style=font-size:inherit;line-height:inherit;word-wrap:inherit!important;word-break:inherit!important>public</span> <span style=font-size:inherit;line-height:inherit;color:#a5da2d;word-wrap:inherit!important;word-break:inherit!important>TreeSet</span><span style=font-size:inherit;line-height:inherit;color:#ff9823;word-wrap:inherit!important;word-break:inherit!important>(Comparator&lt;? <span style=font-size:inherit;line-height:inherit;color:#f82375;word-wrap:inherit!important;word-break:inherit!important>super</span> E&gt; comparator)</span> </span>{<br> &nbsp; &nbsp; &nbsp; &nbsp;<span style=font-size:inherit;line-height:inherit;color:#f82375;word-wrap:inherit!important;word-break:inherit!important>this</span>(<span style=font-size:inherit;line-height:inherit;color:#f82375;word-wrap:inherit!important;word-break:inherit!important>new</span> TreeMap&lt;&gt;(comparator));<br> &nbsp; &nbsp;}<br></code></pre><p style=font-size:inherit;color:inherit;line-height:inherit;margin-top:1.7em;margin-bottom:1.7em>那它有什么用呢？？我们来想一下，当我们想要创建一个<code style="font-size:inherit;line-height:inherit;word-wrap:break-word;padding:2px 4px;border-radius:4px;margin-right:2px;margin-left:2px;color:#f82375;background:#f8f8f8">TreeSet&lt;String&gt;</code>类型的变量的时候，并传入一个可以比较String大小的Comparator。<p style=font-size:inherit;color:inherit;line-height:inherit;margin-top:1.7em;margin-bottom:1.7em>那么这个Comparator的选择就有很多了，<strong style=font-size:inherit;line-height:inherit;color:#e96900>它可以是<code style="font-size:inherit;line-height:inherit;word-wrap:break-word;padding:2px 4px;border-radius:4px;margin-right:2px;margin-left:2px;color:#f82375;background:#f8f8f8">Comparator&lt;String&gt;</code>，还可以是类型参数是String的父类，比如说<code style="font-size:inherit;line-height:inherit;word-wrap:break-word;padding:2px 4px;border-radius:4px;margin-right:2px;margin-left:2px;color:#f82375;background:#f8f8f8">Comparator&lt;Objcet&gt;</code></strong>….<p style=font-size:inherit;color:inherit;line-height:inherit;margin-top:1.7em;margin-bottom:1.7em>这样做，就非常<strong style=font-size:inherit;line-height:inherit;color:#e96900>灵活</strong>了。也就是说，<strong style=font-size:inherit;line-height:inherit;color:#e96900>只要它能够比较字符串大小，就行了</strong><p style=font-size:inherit;color:inherit;line-height:inherit;margin-top:1.7em;margin-bottom:1.7em>值得注意的是：<strong style=font-size:inherit;line-height:inherit;color:#e96900>无论是设定通配符上限还是下限，都是不能操作与对象有关的方法，只要涉及到了通配符，它的类型都是不确定的！</strong><h2 style="color:inherit;line-height:inherit;margin-top:1.6em;margin-bottom:1.6em;font-weight:bold;font-size:1.4em;border-bottom:2px solid #00acc1"><span style=font-size:inherit;line-height:inherit;display:inline-block;font-weight:normal;background:#00acc1;color:#ffffff;padding-top:3px;padding-right:10px;padding-left:10px;border-top-right-radius:3px;border-top-left-radius:4px;margin-right:2px>3.5通配符和泛型方法</span></h2><p style=font-size:inherit;color:inherit;line-height:inherit;margin-top:1.7em;margin-bottom:1.7em><strong style=font-size:inherit;line-height:inherit;color:#e96900>大多时候，我们都可以使用泛型方法来代替通配符的</strong>…..<pre style=font-size:inherit;color:inherit;line-height:inherit;margin-top:0px;margin-bottom:0px;padding:0px><code style=margin-right:2px;margin-left:2px;line-height:15px;font-size:11px;word-spacing:-3px;letter-spacing:0px;font-family:Consolas,Inconsolata,Courier,monospace;border-radius:0px;color:#a9b7c6;background:#282b2e;padding:.5em;display:block!important;word-wrap:normal!important;word-break:normal!important;overflow:auto!important> &nbsp; &nbsp;<span style=font-size:inherit;line-height:inherit;color:gray;word-wrap:inherit!important;word-break:inherit!important>//使用通配符</span><br> &nbsp; &nbsp;<span style=font-size:inherit;line-height:inherit;color:#f82375;word-wrap:inherit!important;word-break:inherit!important><span style=font-size:inherit;line-height:inherit;word-wrap:inherit!important;word-break:inherit!important>public</span> <span style=font-size:inherit;line-height:inherit;word-wrap:inherit!important;word-break:inherit!important>static</span> <span style=font-size:inherit;line-height:inherit;word-wrap:inherit!important;word-break:inherit!important>void</span> <span style=font-size:inherit;line-height:inherit;color:#a5da2d;word-wrap:inherit!important;word-break:inherit!important>test</span><span style=font-size:inherit;line-height:inherit;color:#ff9823;word-wrap:inherit!important;word-break:inherit!important>(List&lt;?&gt; list)</span> </span>{<br><br> &nbsp; &nbsp;}<br><br> &nbsp; &nbsp;<span style=font-size:inherit;line-height:inherit;color:gray;word-wrap:inherit!important;word-break:inherit!important>//使用泛型方法</span><br> &nbsp; &nbsp;<span style=font-size:inherit;line-height:inherit;color:#f82375;word-wrap:inherit!important;word-break:inherit!important>public</span> &lt;T&gt; <span style=font-size:inherit;line-height:inherit;color:#f82375;word-wrap:inherit!important;word-break:inherit!important><span style=font-size:inherit;line-height:inherit;word-wrap:inherit!important;word-break:inherit!important>void</span> &nbsp;<span style=font-size:inherit;line-height:inherit;color:#a5da2d;word-wrap:inherit!important;word-break:inherit!important>test2</span><span style=font-size:inherit;line-height:inherit;color:#ff9823;word-wrap:inherit!important;word-break:inherit!important>(List&lt;T&gt; t)</span> </span>{<br><br> &nbsp; &nbsp;}<br></code></pre><p style=font-size:inherit;color:inherit;line-height:inherit;margin-top:1.7em;margin-bottom:1.7em>上面这两个方法都是可以的…..那么现在问题来了，我们使用通配符还是使用泛型方法呢？？<p style=font-size:inherit;color:inherit;line-height:inherit;margin-top:1.7em;margin-bottom:1.7em>原则：<ul class=list-paddingleft-2><li><p>如果<strong style=font-size:inherit;line-height:inherit;color:#e96900>参数之间的类型有依赖关系</strong>，或者返<strong style=font-size:inherit;line-height:inherit;color:#e96900>回值是与参数之间有依赖关系</strong>的。那么就使用<strong style=font-size:inherit;line-height:inherit;color:#e96900>泛型方法</strong></p><li><p>如果<strong style=font-size:inherit;line-height:inherit;color:#e96900>没有依赖关系</strong>的，就使用<strong style=font-size:inherit;line-height:inherit;color:#e96900>通配符</strong>，通配符会<strong style=font-size:inherit;line-height:inherit;color:#e96900>灵活一些.</strong></p></ul><hr style="font-size:inherit;color:inherit;line-height:inherit;height:8px;margin:2.5rem 20px;border-radius:50%;border-top:3px solid #a5a5a5"><h2 style="color:inherit;line-height:inherit;margin-top:1.6em;margin-bottom:1.6em;font-weight:bold;font-size:1.4em;border-bottom:2px solid #00acc1"><span style=font-size:inherit;line-height:inherit;display:inline-block;font-weight:normal;background:#00acc1;color:#ffffff;padding-top:3px;padding-right:10px;padding-left:10px;border-top-right-radius:3px;border-top-left-radius:4px;margin-right:2px>3.6泛型擦除</span></h2><p style=font-size:inherit;color:inherit;line-height:inherit;margin-top:1.7em;margin-bottom:1.7em>泛型是<strong style=font-size:inherit;line-height:inherit;color:#e96900>提供给javac编译器使用的</strong>，它用于限定集合的输入类型，让编译器在源代码级别上，即挡住向集合中插入非法数据。但编译器编译完带有泛形的java程序后，<strong style=font-size:inherit;line-height:inherit;color:#e96900>生成的class文件中将不再带有泛形信息</strong>，以此使程序运行效率不受到影响，这个过程称之为“擦除”。<h3 style="color:inherit;line-height:inherit;margin-top:1.6em;margin-bottom:1.6em;font-weight:bold;border-bottom:2px solid #ef7060;font-size:1.3em"><span style="font-size:inherit;line-height:inherit;display:inline-block;font-weight:normal;background:#ef7060;color:#ffffff;padding:3px 10px 1px;border-top-right-radius:3px;border-top-left-radius:3px;margin-right:3px">3.6.1兼容性</span><span style="display:inline-block;vertical-align:bottom;border-bottom:36px solid #efebe9;border-right:20px solid transparent"> </span></h3><p style=font-size:inherit;color:inherit;line-height:inherit;margin-top:1.7em;margin-bottom:1.7em>JDK5提出了泛型这个概念，但是JDK5以前是没有泛型的。也就是泛型是需要兼容JDK5以下的集合的。<p style=font-size:inherit;color:inherit;line-height:inherit;margin-top:1.7em;margin-bottom:1.7em><strong style=font-size:inherit;line-height:inherit;color:#e96900>当把带有泛型特性的集合赋值给老版本的集合时候，会把泛型给擦除了。</strong><p style=font-size:inherit;color:inherit;line-height:inherit;margin-top:1.7em;margin-bottom:1.7em>值得注意的是：<strong style=font-size:inherit;line-height:inherit;color:#e96900>它保留的就类型参数的上限。</strong><pre style=font-size:inherit;color:inherit;line-height:inherit;margin-top:0px;margin-bottom:0px;padding:0px><code style=margin-right:2px;margin-left:2px;line-height:15px;font-size:11px;word-spacing:-3px;letter-spacing:0px;font-family:Consolas,Inconsolata,Courier,monospace;border-radius:0px;color:#a9b7c6;background:#282b2e;padding:.5em;display:block!important;word-wrap:normal!important;word-break:normal!important;overflow:auto!important> &nbsp; &nbsp; &nbsp; &nbsp;List&lt;String&gt; list = <span style=font-size:inherit;line-height:inherit;color:#f82375;word-wrap:inherit!important;word-break:inherit!important>new</span> ArrayList&lt;&gt;();<br><br> &nbsp; &nbsp; &nbsp; &nbsp;<span style=font-size:inherit;line-height:inherit;color:gray;word-wrap:inherit!important;word-break:inherit!important>//类型被擦除了，保留的是类型的上限，String的上限就是Object</span><br> &nbsp; &nbsp; &nbsp; &nbsp;List list1 = list;<br></code></pre><p style=font-size:inherit;color:inherit;line-height:inherit;margin-top:1.7em;margin-bottom:1.7em>如果我<strong style=font-size:inherit;line-height:inherit;color:#e96900>把没有类型参数的集合赋值给带有类型参数的集合赋值</strong>，这又会怎么样？？<pre style=font-size:inherit;color:inherit;line-height:inherit;margin-top:0px;margin-bottom:0px;padding:0px><code style=margin-right:2px;margin-left:2px;line-height:15px;font-size:11px;word-spacing:-3px;letter-spacing:0px;font-family:Consolas,Inconsolata,Courier,monospace;border-radius:0px;color:#a9b7c6;background:#282b2e;padding:.5em;display:block!important;word-wrap:normal!important;word-break:normal!important;overflow:auto!important> &nbsp; &nbsp; &nbsp; &nbsp;List list = <span style=font-size:inherit;line-height:inherit;color:#f82375;word-wrap:inherit!important;word-break:inherit!important>new</span> ArrayList();<br> &nbsp; &nbsp; &nbsp; &nbsp;List&lt;String&gt; list2 = list;<br></code></pre><p style=font-size:inherit;color:inherit;line-height:inherit;margin-top:1.7em;margin-bottom:1.7em>它也不会报错，仅仅是提示“未经检查的转换”<hr style="font-size:inherit;color:inherit;line-height:inherit;height:8px;margin:2.5rem 20px;border-radius:50%;border-top:3px solid #a5a5a5"><h1 style="color:inherit;line-height:inherit;margin-top:1.6em;margin-bottom:1.6em;font-weight:bold;font-size:1.6em;border-top:2px solid #039be5"><span style="font-size:inherit;line-height:inherit;display:inline-block;font-weight:normal;background:#039be5;color:#ffffff;padding-top:3px;padding-right:10px;padding-left:10px;border-bottom-right-radius:3px;border-bottom-left-radius:4px;border-bottom:2px solid #006064;border-right:3px solid #006064;margin-left:5px;margin-right:2px">四、泛型的应用</span></h1><p style=font-size:inherit;color:inherit;line-height:inherit;margin-top:1.7em;margin-bottom:1.7em>当我们写网页的时候，常常会有多个DAO，我们要写每次都要写好几个DAO，这样会有点麻烦。<figure style=font-size:inherit;color:inherit;line-height:inherit><img data-ratio=0.3719676549865229 data-src="https://mmbiz.qpic.cn/mmbiz_png/2BGWl1qPxib2qYPoJkJPxUx0guZHy8QibLhDCfSibULbbtngy31jF03CPdHVnicMdu1BImN4r1qBf7aPLjxlALFghQ/640?wx_fmt=png" data-type=png data-w=371 style=font-size:inherit;color:inherit;line-height:inherit;display:block;margin-right:auto;margin-left:auto;width:371px!important;height:auto!important;visibility:visible!important title=这里写图片描述 _width=371px src="" crossorigin=anonymous data-fail=0><figcaption style=line-height:inherit;margin-top:10px;text-align:center;color:#999999;font-size:.7em>这里写图片描述</figcaption></figure><p style=font-size:inherit;color:inherit;line-height:inherit;margin-top:1.7em;margin-bottom:1.7em>那么我们想要的效果是什么呢？？<strong style=font-size:inherit;line-height:inherit;color:#e96900>只写一个抽象DAO，别的DAO只要继承该抽象DAO，就有对应的方法了。</strong><p style=font-size:inherit;color:inherit;line-height:inherit;margin-top:1.7em;margin-bottom:1.7em>要实现这样的效果，肯定是要用到泛型的。<strong style=font-size:inherit;line-height:inherit;color:#e96900>因为在抽象DAO中，是不可能知道哪一个DAO会继承它自己，所以是不知道其具体的类型的。而泛型就是在创建的时候才指定其具体的类型。</strong><ul class=list-paddingleft-2><li><p><span style=font-size:inherit;color:inherit;line-height:inherit>抽象DAO</span></p></ul><pre style=font-size:inherit;color:inherit;line-height:inherit;margin-top:0px;margin-bottom:0px;padding:0px><code style=margin-right:2px;margin-left:2px;line-height:15px;font-size:11px;word-spacing:-3px;letter-spacing:0px;font-family:Consolas,Inconsolata,Courier,monospace;border-radius:0px;color:#a9b7c6;background:#282b2e;padding:.5em;display:block!important;word-wrap:normal!important;word-break:normal!important;overflow:auto!important><span style=font-size:inherit;line-height:inherit;color:#f82375;word-wrap:inherit!important;word-break:inherit!important>public</span> <span style=font-size:inherit;line-height:inherit;color:#f82375;word-wrap:inherit!important;word-break:inherit!important>abstract</span> <span style=font-size:inherit;color:inherit;line-height:inherit;word-wrap:inherit!important;word-break:inherit!important><span style=font-size:inherit;line-height:inherit;color:#f82375;word-wrap:inherit!important;word-break:inherit!important>class</span> <span style=font-size:inherit;line-height:inherit;color:#a5da2d;word-wrap:inherit!important;word-break:inherit!important>BaseDao</span>&lt;<span style=font-size:inherit;line-height:inherit;color:#a5da2d;word-wrap:inherit!important;word-break:inherit!important>T</span>&gt; </span>{<br><br> &nbsp; &nbsp;<span style=font-size:inherit;line-height:inherit;color:gray;word-wrap:inherit!important;word-break:inherit!important>//模拟hibernate....</span><br> &nbsp; &nbsp;<span style=font-size:inherit;line-height:inherit;color:#f82375;word-wrap:inherit!important;word-break:inherit!important>private</span> Session session;<br> &nbsp; &nbsp;<span style=font-size:inherit;line-height:inherit;color:#f82375;word-wrap:inherit!important;word-break:inherit!important>private</span> Class clazz;<br><br><br> &nbsp; &nbsp;<span style=font-size:inherit;line-height:inherit;color:gray;word-wrap:inherit!important;word-break:inherit!important>//哪个子类调的这个方法，得到的class就是子类处理的类型（非常重要）</span><br> &nbsp; &nbsp;<span style=font-size:inherit;line-height:inherit;color:#f82375;word-wrap:inherit!important;word-break:inherit!important><span style=font-size:inherit;line-height:inherit;word-wrap:inherit!important;word-break:inherit!important>public</span> <span style=font-size:inherit;line-height:inherit;color:#a5da2d;word-wrap:inherit!important;word-break:inherit!important>BaseDao</span><span style=font-size:inherit;line-height:inherit;color:#ff9823;word-wrap:inherit!important;word-break:inherit!important>()</span></span>{<br> &nbsp; &nbsp; &nbsp; &nbsp;Class clazz = <span style=font-size:inherit;line-height:inherit;color:#f82375;word-wrap:inherit!important;word-break:inherit!important>this</span>.getClass(); &nbsp;<span style=font-size:inherit;line-height:inherit;color:gray;word-wrap:inherit!important;word-break:inherit!important>//拿到的是子类</span><br> &nbsp; &nbsp; &nbsp; &nbsp;ParameterizedType &nbsp;pt = (ParameterizedType) clazz.getGenericSuperclass(); &nbsp;<span style=font-size:inherit;line-height:inherit;color:gray;word-wrap:inherit!important;word-break:inherit!important>//BaseDao&lt;Category&gt;</span><br> &nbsp; &nbsp; &nbsp; &nbsp;clazz = (Class) pt.getActualTypeArguments()[<span style=font-size:inherit;line-height:inherit;color:#ae87fa;word-wrap:inherit!important;word-break:inherit!important>0</span>];<br> &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(clazz);<br><br> &nbsp; &nbsp;}<br><br><br> &nbsp; &nbsp;<span style=font-size:inherit;line-height:inherit;color:#f82375;word-wrap:inherit!important;word-break:inherit!important><span style=font-size:inherit;line-height:inherit;word-wrap:inherit!important;word-break:inherit!important>public</span> <span style=font-size:inherit;line-height:inherit;word-wrap:inherit!important;word-break:inherit!important>void</span> <span style=font-size:inherit;line-height:inherit;color:#a5da2d;word-wrap:inherit!important;word-break:inherit!important>add</span><span style=font-size:inherit;line-height:inherit;color:#ff9823;word-wrap:inherit!important;word-break:inherit!important>(T t)</span></span>{<br> &nbsp; &nbsp; &nbsp; &nbsp;session.save(t);<br> &nbsp; &nbsp;}<br><br> &nbsp; &nbsp;<span style=font-size:inherit;line-height:inherit;color:#f82375;word-wrap:inherit!important;word-break:inherit!important><span style=font-size:inherit;line-height:inherit;word-wrap:inherit!important;word-break:inherit!important>public</span> T <span style=font-size:inherit;line-height:inherit;color:#a5da2d;word-wrap:inherit!important;word-break:inherit!important>find</span><span style=font-size:inherit;line-height:inherit;color:#ff9823;word-wrap:inherit!important;word-break:inherit!important>(String id)</span></span>{<br> &nbsp; &nbsp; &nbsp; &nbsp;<span style=font-size:inherit;line-height:inherit;color:#f82375;word-wrap:inherit!important;word-break:inherit!important>return</span> (T) session.get(clazz, id);<br> &nbsp; &nbsp;}<br><br> &nbsp; &nbsp;<span style=font-size:inherit;line-height:inherit;color:#f82375;word-wrap:inherit!important;word-break:inherit!important><span style=font-size:inherit;line-height:inherit;word-wrap:inherit!important;word-break:inherit!important>public</span> <span style=font-size:inherit;line-height:inherit;word-wrap:inherit!important;word-break:inherit!important>void</span> <span style=font-size:inherit;line-height:inherit;color:#a5da2d;word-wrap:inherit!important;word-break:inherit!important>update</span><span style=font-size:inherit;line-height:inherit;color:#ff9823;word-wrap:inherit!important;word-break:inherit!important>(T t)</span></span>{<br> &nbsp; &nbsp; &nbsp; &nbsp;session.update(t);<br> &nbsp; &nbsp;}<br><br> &nbsp; &nbsp;<span style=font-size:inherit;line-height:inherit;color:#f82375;word-wrap:inherit!important;word-break:inherit!important><span style=font-size:inherit;line-height:inherit;word-wrap:inherit!important;word-break:inherit!important>public</span> <span style=font-size:inherit;line-height:inherit;word-wrap:inherit!important;word-break:inherit!important>void</span> <span style=font-size:inherit;line-height:inherit;color:#a5da2d;word-wrap:inherit!important;word-break:inherit!important>delete</span><span style=font-size:inherit;line-height:inherit;color:#ff9823;word-wrap:inherit!important;word-break:inherit!important>(String id)</span></span>{<br> &nbsp; &nbsp; &nbsp; &nbsp;T t = (T) session.get(clazz, id);<br> &nbsp; &nbsp; &nbsp; &nbsp;session.delete(t);<br> &nbsp; &nbsp;}<br><br>}<br></code></pre><ul class=list-paddingleft-2><li><p><span style=font-size:inherit;color:inherit;line-height:inherit>继承抽象DAO，该实现类就有对应的增删改查的方法了。</span></p></ul><p style=font-size:inherit;color:inherit;line-height:inherit;margin-top:1.7em;margin-bottom:1.7em>CategoryDao<pre style=font-size:inherit;color:inherit;line-height:inherit;margin-top:0px;margin-bottom:0px;padding:0px><code style=margin-right:2px;margin-left:2px;line-height:15px;font-size:11px;word-spacing:-3px;letter-spacing:0px;font-family:Consolas,Inconsolata,Courier,monospace;border-radius:0px;color:#a9b7c6;background:#282b2e;padding:.5em;display:block!important;word-wrap:normal!important;word-break:normal!important;overflow:auto!important><span style=font-size:inherit;line-height:inherit;color:#f82375;word-wrap:inherit!important;word-break:inherit!important>public</span> <span style=font-size:inherit;color:inherit;line-height:inherit;word-wrap:inherit!important;word-break:inherit!important><span style=font-size:inherit;line-height:inherit;color:#f82375;word-wrap:inherit!important;word-break:inherit!important>class</span> <span style=font-size:inherit;line-height:inherit;color:#a5da2d;word-wrap:inherit!important;word-break:inherit!important>CategoryDao</span> <span style=font-size:inherit;line-height:inherit;color:#f82375;word-wrap:inherit!important;word-break:inherit!important>extends</span> <span style=font-size:inherit;line-height:inherit;color:#a5da2d;word-wrap:inherit!important;word-break:inherit!important>BaseDao</span>&lt;<span style=font-size:inherit;line-height:inherit;color:#a5da2d;word-wrap:inherit!important;word-break:inherit!important>Category</span>&gt; </span>{<br><br>}<br></code></pre><p style=font-size:inherit;color:inherit;line-height:inherit;margin-top:1.7em;margin-bottom:1.7em>BookDao<pre style=font-size:inherit;color:inherit;line-height:inherit;margin-top:0px;margin-bottom:0px;padding:0px><code style=margin-right:2px;margin-left:2px;line-height:15px;font-size:11px;word-spacing:-3px;letter-spacing:0px;font-family:Consolas,Inconsolata,Courier,monospace;border-radius:0px;color:#a9b7c6;background:#282b2e;padding:.5em;display:block!important;word-wrap:normal!important;word-break:normal!important;overflow:auto!important><span style=font-size:inherit;line-height:inherit;color:#f82375;word-wrap:inherit!important;word-break:inherit!important>public</span> <span style=font-size:inherit;color:inherit;line-height:inherit;word-wrap:inherit!important;word-break:inherit!important><span style=font-size:inherit;line-height:inherit;color:#f82375;word-wrap:inherit!important;word-break:inherit!important>class</span> <span style=font-size:inherit;line-height:inherit;color:#a5da2d;word-wrap:inherit!important;word-break:inherit!important>BookDao</span> <span style=font-size:inherit;line-height:inherit;color:#f82375;word-wrap:inherit!important;word-break:inherit!important>extends</span> <span style=font-size:inherit;line-height:inherit;color:#a5da2d;word-wrap:inherit!important;word-break:inherit!important>BaseDao</span>&lt;<span style=font-size:inherit;line-height:inherit;color:#a5da2d;word-wrap:inherit!important;word-break:inherit!important>Book</span>&gt; </span>{<br><br><br>}<br></code></pre><h1 style="color:inherit;line-height:inherit;margin-top:1.6em;margin-bottom:1.6em;font-weight:bold;font-size:1.6em;border-top:2px solid #039be5"><span style="font-size:inherit;line-height:inherit;display:inline-block;font-weight:normal;background:#039be5;color:#ffffff;padding-top:3px;padding-right:10px;padding-left:10px;border-bottom-right-radius:3px;border-bottom-left-radius:4px;border-bottom:2px solid #006064;border-right:3px solid #006064;margin-left:5px;margin-right:2px">五、最后</span></h1><p style=font-size:inherit;color:inherit;line-height:inherit;margin-top:1.7em;margin-bottom:1.7em>泛型的基础就介绍到这里了，如果以后有需要的话再进行深入研究吧～如果觉得该文章帮助到你，不妨点个赞，关注公众号一波～<figure style=font-size:inherit;color:inherit;line-height:inherit><img data-ratio=1 data-src="https://mmbiz.qpic.cn/mmbiz_jpg/2BGWl1qPxib2qYPoJkJPxUx0guZHy8QibLsqqn1PJc3HaWicsh6zDSfVI1wBKBy2cxDRZ3ydGDs2yVKQCEFd1qv7g/640?wx_fmt=jpeg" data-type=jpeg data-w=198 style=font-size:inherit;color:inherit;line-height:inherit;display:block;margin-right:auto;margin-left:auto;width:198px!important;height:auto!important;visibility:visible!important title _width=198px src="" crossorigin=anonymous data-fail=0><figcaption style=line-height:inherit;margin-top:10px;text-align:center;color:#999999;font-size:.7em></figcaption></figure><p style=font-size:inherit;color:inherit;line-height:inherit;margin-top:1.7em;margin-bottom:1.7em>参考资料:<ul class=list-paddingleft-2><li><p><span style=font-size:inherit;color:inherit;line-height:inherit>Core Java</span></p></ul><blockquote style="line-height:inherit;padding:15px 15px 15px 1rem;font-size:.9em;margin-top:1em;margin-bottom:1em;color:#000000;border-left-width:5px;border-left-color:#ef7060;background:#efebe9;overflow:auto;word-wrap:normal;word-break:normal"><p style=font-size:inherit;color:inherit;line-height:inherit>如果文章有错的地方欢迎指正，大家互相交流。习惯在微信看技术文章，想要获取更多的Java资源的同学，可以<strong style=font-size:inherit;line-height:inherit;color:#e96900>关注微信公众号:Java3y</strong></p></blockquote></section><p><br></p>
 </div>
 
 
 <div class=ct_mpda_wrp id=js_sponsor_ad_area style=display:none></div>
 
 <div class=read-more__area id=js_more_read_area style=display:none>
 
 </div>
 </div>
 
 
 
 <ul id=js_hotspot_area class="article_extend_area sf-hidden"></ul>
 
 
<div class=rich_media_tool id=js_toobar3>
 <div class=weui-flex>
 <div class=weui-flex__item>
 
 <div id=js_read_area3 class="media_tool_meta tips_global_primary meta_primary" style=display:none>
 <span id=readTxt>阅读</span>
 <span id=readNum3></span>
 </div>
 </div>
 <span style=display:none class="media_tool_meta meta_extra meta_praise" id=like_old>
 <i class=icon_praise_gray></i><span class=praise_num id=likeNum_old></span>
 </span>
 
 <span style=visibility:hidden class="media_tool_meta meta_extra meta_like" id=like3>
 <button class=like_btn id=js_like_btn> 
 <span id=js_like_wording> 在看</span><span class=like_num id=likeNum3></span>
 </button>
 </span>
 
 </div>
</div>
 
 <div class=like_comment_wrp id=js_like_comment style=display:none>
 <div class=like_comment_inner>
 
 
 </div>
 </div> 
 <div style=display:none id=wow_close_inform>
 <div class=weui-mask></div>
 <div class=weui-dialog>
 
 
 
 </div>
 </div>
<div id=js_like_toast style=display:none>
 <div class=weui-mask_transparent></div>
 <div class=weui-toast>
 
 
 </div>
</div>
<div style=display:none id=js_comment_panel>
 <div class="like_comment_primary_wrp editing" id=js_comment_wrp>
 
 </div> 
 <div class=like_comment_primary_mask id=js_mask_2></div>
</div>
<div id=js_loading style=display:none>
 <div class=weui-mask_transparent></div>
 <div class=weui-toast>
 
 
 </div>
</div>
 </div>
 </div>
 <div class="rich_media_area_primary sougou" id=sg_tj style=display:none></div>
 
 <div class=rich_media_area_extra>
 <div class=rich_media_area_extra_inner>
 
 <div id=js_share_appmsg>
 </div>
 
 
 <div class=mpda_bottom_container id=js_bottom_ad_area style=display:none></div>
 
 <div id=js_iframetest style=display:none></div>
 
 <div class="rich_media_extra rich_media_extra_discuss" id=js_cmt_container style=display:none>
 
 
 <div class=discuss_mod id=js_friend_cmt_area style=display:none>
 
 
 
 </div>
 <div class=discuss_mod id=js_cmt_area style=display:none>
 </div>
 </div>
 </div>
 </div>
 
 <div id=js_pc_qr_code class=qr_code_pc_outer style=display:block>
 <div class=qr_code_pc_inner>
 <div class=qr_code_pc>
 <img id=js_pc_qr_code_img class=qr_code_pc_img src="">
 <p>微信扫一扫<br>关注该公众号</p>
 </div>
 </div>
 </div>
 </div>
</div>
<div id=js_pc_weapp_code class="weui-desktop-popover weui-desktop-popover_pos-up-center weui-desktop-popover_img-text" style=display:none>
 <div class=weui-desktop-popover__content>
 
 </div>
</div>
<div id=js_minipro_dialog style=display:none>
 <div class=weui-mask></div>
 <div class="weui-dialog weui-dialog_link">
 
 
 
 
 </div>
</div>
<div id=js_link_dialog style=display:none>
 <div class=weui-mask></div>
 <div class="weui-dialog weui-dialog_link">
 
 
 
 
 </div>
</div>
<div class=comment_primary_emotion_panel_wrp id=js_emotion_panel_pc style=display:none>
 <div class=comment_primary_emotion_panel>
 
 </div>
</div>
<div class=weui-dialog__wrp id=js_alert_panel style=display:none>
 <div class=weui-mask></div>
 <div class=weui-dialog>
 
 
 </div>
</div>
<div id=js_weapp_without_auth_dialog style=display:none>
 <div class=weui-mask></div>
 <div class="weui-dialog weui-dialog_link">
 
 
 </div>
</div>
 
 
 
 
 
 
